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

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

相关推荐
phoenix5 小时前
Codable 宏让 Swift 序列化如此简单!
swift
leluckys14 小时前
swift-08-属性、汇编分析inout本质
开发语言·汇编·swift
leluckys17 小时前
swift-oc和swift block和代理
开发语言·ios·swift
leonyie20221 天前
7.3 在通知中显示图片或视频(UNNotificationAttachment)
macos·ios·swiftui·音视频·cocoa·swift
依旧风轻1 天前
使用 Swift 实现 LRU 缓存淘汰策略
缓存·swift·哈希表·lru·双向链表
Swift社区2 天前
Swift 解 LeetCode 250:搞懂同值子树,用递归写出权限系统检查器
开发语言·leetcode·swift
大熊猫侯佩2 天前
“群芳争艳”:CoreData 4 种方法计算最大值的效率比较(上)
swift·排序·sort·array·coredata·nsarray·最大值 max
东坡肘子2 天前
切勿将辅助驾驶宣传成智能驾驶 | 肘子的 Swift 周报 #078
人工智能·swiftui·swift
一牛3 天前
Metal 进阶:读取可绘制对象的像素
ios·swift·计算机图形学
Andy_shenzl4 天前
16、Swift框架微调实战(1)-自我认知数据LORA微调
lora·微调·swift