Hello: I am trying to load an NSPopupButton from a property list, but running the code below results in a fatal error: "Unexpectedly found nil while implicitly unwrapping an Optional value" on line i4. The debug output shows the following:
figures count 4
viewDidLoad() figures count 4
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/wlionelwilliams/Desktop/ScorcentMasterReview/ScorcentMasterReview/FiguresViewController.swift, line 109
Line 10 seems to suggest that the items for the Popupbutton list is available. What am I missing?
overridefunc viewDidLoad() {
super.viewDidLoad()
if let filePath = Bundle.main.path(forResource: "TFigures", ofType: "plist") {
print("filePath", filePath)
figures = Figure.figuresList(filePath)
print("figures count", figures.count)
}
// figuresButton.removeAllItems()
print(#function, "figures count", figures.count)
for figure in figures {
figuresButton.addItem(withTitle:figure.title)
}
print ("itemsCount", title!.count)
selectedFigure = figures [0]
figuresButton.selectItem(at: 0)
}
Thanks.
So, crash is on
figuresButton.addItem(withTitle:figure.title) // Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Could come from:
- figuresButton. (most likely)
- figure.title
Need to check if nil or not:
for figure in figures {
print("figure", figure, "figureButton", figuresButton)
print("item to add", figure.title)
figuresButton.addItem(withTitle:figure.title) // Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
print("added item", figure.title)
}
My guess is that lines 2 and 3 will print once, with figureButton nil.
If so, we'll have to find why.