arrays - 在 Swift 中改变 For 循环的范围
问题描述
我正在尝试设置一个动态范围,该范围在 for 循环内迭代时会发生变化。又名:从索引跳转到索引。
例如:
func func(c: [Int]) -> Int {
var currentIndex = 0
for i in currentIndex..<c.count
currentIndex += 3 //After changing the currentIndex value, will my "i" start from currentIndex?
}
所以我从 0 开始,然后是 3,然后是 6,依此类推......当我运行这段代码时,“i”像往常一样总结为 i = 0、i = 1、i = 2......我可以吗?改变迭代范围?
解决方案
与评论一样,一个强大的解决方案是使用 build in stride
。另一种解决方案是
while currentIndex < c.count
{
//your loop logic
currentIndex += 3
}
你不需要一个“我”。
推荐阅读
- java - java.lang.UnsupportedClassVersionError - 在 MAC OSX 上 - 不支持的类文件主要版本 56
- python - 无效的语法 (
,第 89 行)-pylint(语法错误) - sql - 如果不是使用 SQL 的另一个表,如何从表中删除列?
- javascript - 无法在 Ionic 3 应用程序中导入 Amplitude SDK
- swift - 将结果转换为数组时的性能
- vb.net - 如何解析包含 Unicode ID 的字符串以及纯文本以在数据网格视图中显示
- c++ - wxWidgets:如何改变和显示sizer的内容
- c# - 如何在 C# Windows 窗体中打开可从所有设备访问的 localhost 服务器
- ruby-on-rails - 未发送设计可确认的电子邮件
- mysql - Mysql 中的默认键约束是什么,或者单独的“key”关键字有什么作用?