Hello, all. First post here. I am a rank beginner at Swift, so please pardon the (probably) simplistic question. I have begun working through the SwiftUI for Masterminds book (3rd Ed.) and am stumped about something. In the introduction to structs, they offer the following example (Listing 3-31, pg. 44):
struct Price {
var USD = 0.0
var CAD = 0.0
}
struct Item {
var name: String = "Not defined"
var price: Price?
}
var purchase = Item()
purchase.name = "Lamps"
purchase.price?.USD = 10.50
I am confused on a couple points. First, in the results window, it shows that the value after the last assignment statement is still nil rather than 10.50. Secondly, when I tried to print this value with the command print(purchase.price!.USD), I get an execution error: " Fatal error: Unexpectedly found nil while unwrapping an Optional value". So it appears the assignment is not working? If I don't reference one struct from another and just combine the members in one struct, I can get it to work as I think it should, so seems to have something to do with the dot notation between the two? I don't know.... any help appreciated.