为什么continue
标记错误:
只允许在循环中继续
private func addToUnloadedImagesRow(row: Int, forLocation:String!) {
guard unloadedImagesRows[forLocation] != nil else {
unloadedImagesRows[forLocation] = [Int]()
continue
}
unloadedImagesRows[forLocation]!.append(row)
}
我该如何解决这个问题?
答案 0 :(得分:2)
您不能在保护声明后“继续”当前范围(在您的情况下,addToUnloadedImagesRow(_:forLocation:)
方法)。保护声明的else-block 必须离开当前范围。
无论如何,看看你的代码,我想你只想这样做:
private func addToUnloadedImagesRow(row: Int, forLocation:String!) {
if unloadedImagesRows[forLocation] == nil {
unloadedImagesRows[forLocation] = [Int]()
}
unloadedImagesRows[forLocation]!.append(row)
}
答案 1 :(得分:1)
continue
语句应在loops
中使用。
如果您想检查多个条件,则应使用if
语句而不是guard
。
答案 2 :(得分:1)
如果你真的想使用guard
,你可以这样做:
private func addToUnloadedImagesRow(row: Int, forLocation:String!) {
guard unloadedImagesRows[forLocation] != nil else {
unloadedImagesRows[forLocation] = [row]
return
}
unloadedImagesRows[forLocation]!.append(row)
}
但就个人而言,我发现if
稍微容易阅读。