If I have a class Button:
class Button: Hashable {
private let id: String
var name: String
var rect: CGRect
}
I also have an array of Button: var buttons: [Button], how would I merge every overlapping button in the buttons array? CGRect has an intersects() -> Bool and union() -> CGRect functions.
I tried this and was getting index out of range errors:
for i in 0..<buttons.count {
for j in (i+1)..<buttons.count {
if buttons[i].rect.intersects(buttons[j].rect) {
let secondRect = buttons[j].rect
let union = buttons[i].rect.union(secondRect)
buttons[i].rect = union
buttons.remove(at: j)
}
}
}
Swift
forloop is static, which means that range of the index determined at the start. Whilebuttons.countkeeps decreasing when you remove a next item, bothiandjare counting til the startbuttons.count, that's why you get a crash.There's no dynamic
forin swift (c-like), that's why you have to use while instead: