美文网首页Swift
Non-constant range: argument mus

Non-constant range: argument mus

作者: survivorsfyh | 来源:发表于2023-10-23 18:28 被阅读0次

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

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

相关文章

网友评论

    本文标题:Non-constant range: argument mus

    本文链接:https://www.haomeiwen.com/subject/gpjjidtx.html