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

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

相关推荐
hhb_61814 小时前
Swift核心技术难点与实战案例解析
开发语言·ios·swift
bukeyiwanshui14 小时前
20260518 Swift实验
git·swift
人月神话-Lee15 小时前
【图像处理】饱和度——颜色的浓淡与灰度化
图像处理·人工智能·ios·ai编程·swift
潮起鲸落入海17 小时前
OpenStack块存储管理-cinder对象存储-swift
openstack·swift
人月神话-Lee18 小时前
【图像处理】卷积原理与卷积核——图像处理的核心引擎
图像处理·深度学习·ios·ai编程·swift
Daniel_Coder21 小时前
iOS Widget 开发-15:Widget 性能优化指南
ios·swift·widget·widgetcenter
sakiko_1 天前
Swift学习笔记30-数据库SQlite语句
数据库·学习·swift
Daniel_Coder1 天前
iOS Widget 开发-16:Widget 网络数据加载策略
ios·swift·widget·widgetcenter
看谷秀2 天前
swift Part 1
swift
Daniel_Coder2 天前
iOS Widget 开发-14:iOS 18 控制中心组件开发
ios·swift·widget·activitykit·widgetkit·控制中心组件