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

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

相关推荐
营赢盈英2 天前
OpenAI GPT-3 API error: “You must provide a model parameter“
chatgpt·gpt-3·openai·swift
一只不会编程的猫3 天前
高德地图绘图,点标记,并计算中心点
开发语言·ios·swift
loongloongz3 天前
Swift语言基础教程、Swift练手小项目、Swift知识点实例化学习
开发语言·学习·swift
2401_858120537 天前
深入理解 Swift 中的隐式解包可选类型(Implicitly Unwrapped Optionals)
开发语言·ios·swift
quaer7 天前
QT chart案例
开发语言·qt·swift
安和昂7 天前
【iOS】UIViewController的生命周期
ios·xcode·swift
00圈圈7 天前
Swift 创建扩展(Extension)
ios·swift·extension
2401_858120537 天前
Swift 中的函数:定义、使用与实践指南
开发语言·ssh·swift
quaer8 天前
VS+QT--实现二进制和十进制的转换(含分数部分)
java·开发语言·python·qt·swift
BruceGerGer9 天前
flutter开发实战-flutter web加载html及HtmlElementView的使用
flutter·1024程序员节