for 循环的退出迭代 Swift iOS
Exit iteration of for loop Swift iOS
我有一个函数,里面有一个 for 循环:
func example() {
// create tasks
for link in links {
let currIndex = links.indexOf(link)
if let im = story_cache?.objectForKey(link) as? UIImage {
if ((currIndex != nil) && (currIndex < content.count)) {
if (content[currIndex!].resource_type == "image") {
content[currIndex!].image = im
return
}
}
} else {
if ((currIndex != nil) && (currIndex < content.count)) {
if (content[currIndex!].resource_type == "video") {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory : NSString = paths[0]
let appFile = documentsDirectory.stringByAppendingPathComponent(content[currIndex!].id! + ".mov")
let local_URL = NSURL(fileURLWithPath: appFile)
if let cached_URL = story_cache?.objectForKey(local_URL) as? NSURL {
content[currIndex!].videoURL = cached_URL
return
}
}
}
}
let dltask = session.dataTaskWithURL(link, completionHandler: { (data, response, error) in
// MORE CODE.....
})
}
}
基本上我想要实现的是,如果我们到达任何 return
语句,代码将在循环中完成针对该特定 link
的执行,然后循环移动到下一个 link。如果达到 return 语句中的 NONE,我希望执行 dltask
。我可以使用一堆 else 语句来实现这一点,但我认为这会使代码变得非常混乱。我使用 return
这样做对吗?
您可以使用break outer
或仅使用break
退出循环语句并执行dltask。
希望对您有所帮助。
您正在寻找 continue
:
The continue statement tells a loop to stop what it is doing and start
again at the beginning of the next iteration through the loop. It says
“I am done with the current loop iteration” without leaving the loop
altogether.
只需将 return
替换为 continue
,它将返回到 for
循环,然后 运行 再次进入下一个 link。
我有一个函数,里面有一个 for 循环:
func example() {
// create tasks
for link in links {
let currIndex = links.indexOf(link)
if let im = story_cache?.objectForKey(link) as? UIImage {
if ((currIndex != nil) && (currIndex < content.count)) {
if (content[currIndex!].resource_type == "image") {
content[currIndex!].image = im
return
}
}
} else {
if ((currIndex != nil) && (currIndex < content.count)) {
if (content[currIndex!].resource_type == "video") {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory : NSString = paths[0]
let appFile = documentsDirectory.stringByAppendingPathComponent(content[currIndex!].id! + ".mov")
let local_URL = NSURL(fileURLWithPath: appFile)
if let cached_URL = story_cache?.objectForKey(local_URL) as? NSURL {
content[currIndex!].videoURL = cached_URL
return
}
}
}
}
let dltask = session.dataTaskWithURL(link, completionHandler: { (data, response, error) in
// MORE CODE.....
})
}
}
基本上我想要实现的是,如果我们到达任何 return
语句,代码将在循环中完成针对该特定 link
的执行,然后循环移动到下一个 link。如果达到 return 语句中的 NONE,我希望执行 dltask
。我可以使用一堆 else 语句来实现这一点,但我认为这会使代码变得非常混乱。我使用 return
这样做对吗?
您可以使用break outer
或仅使用break
退出循环语句并执行dltask。
希望对您有所帮助。
您正在寻找 continue
:
The continue statement tells a loop to stop what it is doing and start again at the beginning of the next iteration through the loop. It says “I am done with the current loop iteration” without leaving the loop altogether.
只需将 return
替换为 continue
,它将返回到 for
循环,然后 运行 再次进入下一个 link。