Reputation: 701
I know this is going to be super elementary, but I have this piece of code:
var labels: [String]?
func initVC(image: Images){
self.image = image
let tempLabels = image.label?.allObjects as! [Labels]
for i in 0..<tempLabels.count{
labels?.append(tempLabels[i].label!)
}
}
labels is in the public scope, so the function should have access to it, but when the loop runs through, labels is still nil with no elements.
When I po during debugging, tempLabels is as I expect it to be with 2 string elements.
I'm pretty sure this is a very simple problem, but I guess I'm just out of it right now.
Upvotes: 13
Views: 12399
Reputation: 2542
You are declaring the labels variable but never allowing it to store information. This means that it does not necessarily exist, since it is not initialized, and therefore cannot be used.
For it to be useable, you must initialize it
var labels:[String] = []
Upvotes: 3
Reputation: 701
Yep, it was super simple.
Changed
var labels: [String]?
To
var labels = [String]()
Upvotes: -7
Reputation: 956
Labels has never been initialised. Change
var labels:[String]?
to
var labels:[String] = []
Upvotes: 37