更新 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])
- }
以上便是此次分享的全部内容,希望能对大家有所帮助!