Non-constant range: argument must be an integer literal

更新 Xcode IDE 后 ForEach 方法抛出了如下异常

复制代码
Non-constant range: argument must be an integer literal

新增了指向性 id 参数 init(_:content:)

原始方法

复制代码
ForEach(0 ..< pickerTitleColors.count) {
    Text(self.pickerTitleColors[$0]).tag($0).foregroundColor(self.pickerStyleColors[$0])
}

改进后

复制代码
// 方式一
ForEach(0 ..< pickerTitleColors.count, id:\.self) {
    Text(self.pickerTitleColors[$0]).tag($0).foregroundColor(self.pickerStyleColors[$0])
}

// 方式二
ForEach(0 ..< pickerTitleColors.count, id:\.self) { item in
    Text(self.pickerTitleColors[item]).tag(item).foregroundColor(self.pickerStyleColors[item])
}

// 方式三
ForEach(pickerTitleColors.indices, id:\.self) { item in
    Text(self.pickerTitleColors[item]).tag(item).foregroundColor(self.pickerStyleColors[item])
}

以上便是此次分享的全部内容,希望能对大家有所帮助!

相关推荐
QWQ___qwq1 天前
Swift中.gesture的用法
服务器·microsoft·swift
QWQ___qwq1 天前
SwiftUI 布局之美:Padding 让界面呼吸感拉满
ios·swiftui·swift
用户091 天前
Xcode 26 的10个新特性解析
ios·面试·swift
白熊1882 天前
【图像大模型】ms-swift 深度解析:一站式多模态大模型微调与部署框架的全流程使用指南
开发语言·ios·swift
YGGP3 天前
【Swift】LeetCode 1. 两数之和
swift
2501_915909064 天前
原生 iOS 开发全流程实战,Swift 技术栈、工程结构、自动化上传与上架发布指南
android·ios·小程序·uni-app·自动化·iphone·swift
大熊猫侯佩4 天前
月球矩阵日志:Swift 6.2 主线程隔离抉择(下)
swift·编程语言·apple
大熊猫侯佩4 天前
月球矩阵日志:Swift 6.2 主线程隔离抉择(上)
swift·编程语言·apple
HarderCoder4 天前
Swift 并发深度指南:非结构化任务与分离任务全解析
swift
HarderCoder4 天前
Swift 6 新关键字 `sending` 深度指南——从 `@Sendable` 到 `sending` 的进化之路
swift