Hello Claude31:
I checked the connection. It shows connected in the Connections Inspector.
The line I indicated is throwing the fatal error: line 80 for complete code.
Here is the full code:
import Cocoa
class FiguresViewController: NSViewController {
var figures = [Figure]()
var selectedFigure: Figure?
var figureId:String = ""
@IBOutlet weak var figureImage: NSImageView!
var selectedProduct: Product? {
didSet {
updateUI()
}
}
override func viewWillAppear() {
super.viewWillAppear()
updateUI()
}
func updateUI() {
//1
if isViewLoaded {
if let figure = selectedFigure {
figureImage.image = figure.image
}
}
}
@IBAction func valueChanged(_ sender: NSPopUpButton) {
if let _ = sender.selectedItem?.title,
let index = figures.firstIndex(where: {$0.title == title}) {
selectedFigure = figures[index]
}
}
@IBOutlet weak var figuresButton: NSPopUpButton!
struct Figure {
var selectedFigure: Figures?
let title: String
var image: NSImage? {
get {
let i = NSImage(named: figureName)
return i
}
}
fileprivate let figureName: String
static func figuresList(_ filePath: String) -> [Figure] {
var figures = [Figure]()
if let figuresList = NSArray(contentsOfFile: filePath) as? [[String: Any]] {
print("figuresList ##START##", figuresList, "figuresList ##END##")
for dict in figuresList {
if let figure = Figure(dictionary: dict) {
figures.append(figure)
}
}
}
print("Figures is", figures)
return figures
}
init?(dictionary: [String: Any]) {
guard let title = dictionary["figureId"] as? String,
let figureName = dictionary["figureName"] as? String else {
print("Initialization of Product failed with \(dictionary)")
return nil
}
self.title = title
self.figureName = figureName
}
}
override func 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)Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
}
print ("itemsCount", title!.count)
selectedFigure = figures [0]
figuresButton.selectItem(at: 0)
}
} class Figures: NSViewController {
override func viewDidLoad() {
} super.viewDidLoad()
// Do view setup here.
Here is the Console Output:
filePath /Users/wlionelwilliams/Library/Developer/Xcode/DerivedData/ScorcentMasterReview-gnzfghwosuggdydtgxnsezjfynal/Build/Products/Debug/ScorcentMasterReview.app/Contents/Resources/TFigures.plist
figuresList ##START## [["figureName": SCFigures2, "figureId": 3312], ["figureId": 3313, "figureName": SCFigures4], ["figureId": 3314, "figureName": SCFigures6], ["figureId": 3315, "figureName": SCFigures8]] figuresList ##END##
Figures is [ScorcentMasterReview.FiguresViewController.Figure(selectedFigure: nil, title: "3312", figureName: "SCFigures2"), ScorcentMasterReview.FiguresViewController.Figure(selectedFigure: nil, title: "3313", figureName: "SCFigures4"), ScorcentMasterReview.FiguresViewController.Figure(selectedFigure: nil, title: "3314", figureName: "SCFigures6"), ScorcentMasterReview.FiguresViewController.Figure(selectedFigure: nil, title: "3315", figureName: "SCFigures8")]
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