ios - 在进入 for 循环之前是否有一种检查数组大小的简短方法?

标签 ios swift

<分区>

目前我是这样使用的:

if json.count>0{
   for i in 0...json.count-1{

   }
}

是否有更好、更短的方法来安全地启动 for 循环?

最佳答案

您的代码片段与

for i in 0 ..< json.count { ... }

零次执行循环没有惩罚。

更好

for element in json { /* do stuff with element */ }

或者如果您需要索引:

for (i, element) in json.enumerate() { }

关于ios - 在进入 for 循环之前是否有一种检查数组大小的简短方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37210181/

相关文章:

ios - 部分呈现的 UITableView header

android - Google Cloud Messaging 上的最大主题数

ios - subview 未使用容器的完整 View

swift - 如何将导航栏后退按钮图像设置为默认符号?

ios - 无法使用 blogger API 找到最新的谷歌博客

ios - 字典取零

swift - 有没有办法阻止 UILabel 作为 UIView 进行类型检查?

Swift - 将日期字符串从 "datepicker filled date field"转换为 nsdate

swift - 在 Swift 中,什么是 DictionaryLiteralConvertible?

ios - 使用 ViewController 显示 UIDatePicker (modalPresentationStyle = UIModalPresentationPopover)