Ios 迭代数组时的快速无限循环
我有一个协议定义为Ios 迭代数组时的快速无限循环,ios,arrays,swift,loops,Ios,Arrays,Swift,Loops,我有一个协议定义为 @objc protocol MyDatasource : class { var currentReportListObjects:[ReportListObject] { get }; } 还有一些代码可以在Swift中迭代返回的数组(作为来自ObjC的NSArray)作为 if let reportListObjects = datasource?.currentReportListObjects { for reportListObject:Repo
@objc protocol MyDatasource : class {
var currentReportListObjects:[ReportListObject] { get };
}
还有一些代码可以在Swift中迭代返回的数组(作为来自ObjC的NSArray)作为
if let reportListObjects = datasource?.currentReportListObjects {
for reportListObject:ReportListObject? in reportListObjects {
if let report = reportListObject {
// Do something useful with 'report'
}
}
}
如果我的reportListObjects数组为nil,我会在for循环中陷入无限循环。同样,如果数组中有数据,则会对其进行迭代并执行“有用的操作”,直到到达数组的末尾,但循环不会中断并无限继续
我做错了什么?还是我在这里遗漏了一些显而易见的东西?您在这里添加了很多额外的选项,这些选项让人困惑。你的意思是:
for report in datasource?.currentReportListObjects ?? [] {
// Do something useful with 'report'
}
如果datasource
有一个值,这将迭代其currentReportListObjects
。否则,它将遍历空列表
如果您确实想打破它,而不是使用?
,那么您的意思是:
if let reportListObjects = datasource?.currentReportListObjects {
for report in reportListObjects {
println(report)
}
}
不需要中间的
reportListObject:reportListObject?
(这就是问题的根源,因为它接受nil
,这是生成器通常的终止)。您在这里添加了很多额外的选项,这些选项会让事情变得混乱。你的意思是:
for report in datasource?.currentReportListObjects ?? [] {
// Do something useful with 'report'
}
如果datasource
有一个值,这将迭代其currentReportListObjects
。否则,它将遍历空列表
如果您确实想打破它,而不是使用?
,那么您的意思是:
if let reportListObjects = datasource?.currentReportListObjects {
for report in reportListObjects {
println(report)
}
}
不需要中间的
reportListObject:reportListObject?
(这就是问题的根源,因为它接受nil
,这是生成器通常的终止)。您在这里添加了很多额外的选项,这些选项会让事情变得混乱。你的意思是:
for report in datasource?.currentReportListObjects ?? [] {
// Do something useful with 'report'
}
如果datasource
有一个值,这将迭代其currentReportListObjects
。否则,它将遍历空列表
如果您确实想打破它,而不是使用?
,那么您的意思是:
if let reportListObjects = datasource?.currentReportListObjects {
for report in reportListObjects {
println(report)
}
}
不需要中间的
reportListObject:reportListObject?
(这就是问题的根源,因为它接受nil
,这是生成器通常的终止)。您在这里添加了很多额外的选项,这些选项会让事情变得混乱。你的意思是:
for report in datasource?.currentReportListObjects ?? [] {
// Do something useful with 'report'
}
如果datasource
有一个值,这将迭代其currentReportListObjects
。否则,它将遍历空列表
如果您确实想打破它,而不是使用?
,那么您的意思是:
if let reportListObjects = datasource?.currentReportListObjects {
for report in reportListObjects {
println(report)
}
}
不需要中间的
reportListObject:reportListObject?
(这就是问题的根源,因为它接受nil
,这是生成器的常用终止)。数组的类型为reportListObject
(非可选),但是你是的
-在
中说ReportListObject?
。。。而且NSArray
可能是nil
终止的。。。将-中的的中的类型更改为非可选(或者让它隐式推断类型)?谢谢。成功了。可能会落入一个危险的陷阱!!!您的数组类型为ReportListObject
(非可选),但您是中的-表示ReportListObject?
。。。而且NSArray
可能是nil
终止的。。。将
-
中的
的中的类型更改为非可选(或者让它隐式推断类型)?谢谢。成功了。可能会落入一个危险的陷阱!!!您的数组类型为ReportListObject
(非可选),但您是中的-表示ReportListObject?
。。。而且NSArray
可能是nil
终止的。。。将
-
中的
的中的类型更改为非可选(或者让它隐式推断类型)?谢谢。成功了。可能会落入一个危险的陷阱!!!您的数组类型为ReportListObject
(非可选),但您是中的-表示ReportListObject?
。。。而且NSArray
可能是nil
终止的。。。将
-
中的
的中的类型更改为非可选(或者让它隐式推断类型)?谢谢。成功了。可能会落入一个危险的陷阱!!!