Breaking labeled scopes
break is used to exit the current scope, for example a for loop:
for i in 1...10 {
guard i < 9 else { break }
// Code here.
}But what if we have nested loops, and we want to break a specific one? Swift has us covered, with labels:
outer: for i in 1...10 {
inner: for j in 1...10 {
guard i < 9 else { break outer }
guard j < 8 else { break inner }
// Code here.
}
}But today I found something interesting: you can label if blocks, as well:
// Works with non optional binding ifs as well.
abCheck: if let a = b() {
guard a.condition else { break abCheck }
// Code here.
}