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

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

相关推荐
Yorelee.2 小时前
ms-swift在训练时遇到的部分问题及解决方案
开发语言·nlp·transformer·swift
yBmZlQzJ18 小时前
免费内网穿透-端口转发配置介绍
运维·经验分享·docker·容器·1024程序员节
崽崽长肉肉1 天前
swift中的知识总结(一)
ios·swift
Yakamoz1 天前
Swift Array的写时复制
swift
汉秋1 天前
SwiftUI 中的 compositingGroup():真正含义与渲染原理
swiftui·swift
汉秋1 天前
SwiftUI 中的 @ViewBuilder 全面解析
swiftui·swift
胖虎12 天前
SwiftUI 页面作为一级页面数据被重置问题分析
ios·swiftui·swift·state·observedobject·stateobject·swiftui页面生命周期
健了个平_242 天前
【iOS】如何在 iOS 26 的UITabBarController中使用自定义TabBar
ios·swift·wwdc
金融小师妹2 天前
AI算法视角下非农夜冲击波来袭,黄金高位区间震荡态势的深度神经网络解析
大数据·深度学习·1024程序员节
1024小神2 天前
xcode 配置了AppIcon 但是不显示icon图标
ios·swiftui·swift