I have this piece of code which works like a charm:
As I also have a previousGalleryItem function I came up with the idea to add a computed property:
Then my code stops working. It throws me the error that it can not unwrap the optional. It highlights the code
. I do not understand why.
Code:
var gallery: Gallery?
var currentGalleryItem: GalleryItem?
func buildScreen() {
if let currentItem = self.currentGalleryItem {
// the user may not have set only a thumbnail and no photo
if let currentPhoto = currentItem.photo.photo {
self.imageView.image = UIImage(data: currentPhoto)
}
} else {
self.imageView.image = UIImage(named: "addImage")
}
}
func showNextGalleryItem() {
if let currentItem = self.currentGalleryItem {
self.currentGalleryItem = self.gallery?.nextGalleryItem(currentItem.indexInTheArray)
self.buildScreen()
} else {
self.noPhotosAlert()
}
}
As I also have a previousGalleryItem function I came up with the idea to add a computed property:
Code:
var currentGalleryItem: GalleryItem?
{
didSet {
self.buildScreen()
}
}
Then my code stops working. It throws me the error that it can not unwrap the optional. It highlights the code
Code:
self.imageView.image = UIImage(data: currentPhoto)