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])
}

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

相关推荐
sweet丶2 天前
Swift 元编程-Macro
swift
影寂ldy5 天前
WinForm PictureBox控件 + ImageList组件 完整笔记
开发语言·笔记·swift
Deepzz6 天前
macOS 上调教第三方鼠标的一些经验:从滚动顺滑到输入法自动切换
macos·swift·鼠标
东坡肘子6 天前
WWDC 26:AI 帮你看完了,然后呢?-- 肘子的 Swift 周报 #140
人工智能·swiftui·swift
大熊猫侯佩7 天前
SwiftData 迁移深度指南:从入门到“填坑”(下集)
数据库·swift·编程语言
大熊猫侯佩7 天前
SwiftData 迁移深度指南:从入门到“填坑”(上集)
数据库·swift·编程语言
多彩电脑7 天前
SwiftUI的导航界面的嵌套问题
开发语言·swift·设计语言
wjm0410068 天前
ios内存管理
ios·objective-c·swift·客户端开发
开开心心就好8 天前
解决截图被拦截黑屏问题的免费小工具
安全·智能手机·flink·kafka·pdf·音视频·1024程序员节
大熊猫侯佩8 天前
Swift 6.4 的 Ref / MutableRef 大揭秘:给值类型开一扇“安全的小窗”
ios·swift·编程语言