I have an array of 4 UIImageVIew and array of images of variable size: [UIImage].
I would like to iterate through the array of views and assign 4 last images to each view.
let photos = images.suffix(4)
for (index, view) in views.enumerate() {
if index <= photos.count - 1 {
view.image = photos[index]
} else {
view.image = nil
}
}
Code works perfectly if there are no more than 4 elements in images. Then I got a crash: fatal error: ArraySlice index out of range.
I printed out index in for-loop and it seems that crash happens when index is 0. views.count and photos.count return 4 in the same loop.
So, I get an error when accessing the first - 0 element of an non-empty array.
In Swift 1.2 this code worked flawlessly:
let photos = suffix(images, 4)
for (index, view) in enumerate(views) {
if index <= photos.count - 1 {
view.image = photos[index]
} else {
view.image = nil
}
}
The only change after switching to 2.0 is in suffix and enumerate methods.
Does it look like a Swift 2.0 bug or am I doing something wrong? Link to source file
Solution I've finished with:
let photos = Array(images.suffix(4))
Should work exactly as suffix() function in Swift 1.2