I'm struggling with a for-in-loop in Swift. I have two for loops and I expected them be equivalent but the first gives an error and the second works as I expected. Can someone explain me why it's working differently?
protocol Slide {
var title: String { get set }
}
class BasicSlide: NSObject, Slide {
var title: String = "Title"
}
var slides: [Slide]?
slides = [BasicSlide()]
for slide in slides! {
slide.title = "New title" // Cannot assign to property: 'slide' is a 'let' constant
}
for var i in 0 ..< slides!.count {
slides![i].title = "New title"
}