[NSObject : AnyObject]? v. [NSObject: AnyObject]!

On p. 122 the return value of the class func shown on the last line of the page has the type:

As discussed on p. 57, the type:

…is the type of an NSDictionary that Swift has partially cast for us. The authors explain (on p. 122) that the class func returns an optional dictionary:

…because there might not be a dictionary corresponding to the voice name argument that you pass to the class func–in that case the class func would return nil instead of a dictionary.

But on p. 57, the NSProcessInfo class purportedly has an NSDictionary property that gets interpreted by Swift as the type:

Note the !. I looked up the NSProcessInfo class in the docs, and it says the Swift type is actually:

Note that the type of the environment variable is not even an optional type. To me, declaring the environment variable as an implicitly unwrapped optional type, as the book does, means, “Hey, Cocoa needs to initialize the environment variable to nil, but by the time you are able to interact with the environment variable, it will be assigned a dictionary, so you don’t need to worry about unwrapping the optional–it will be forcibly unwrapped for you any time you use the variable.” As a result, I wonder why that variable has to be set to nil before a dictionary can be assigned to it. Did the authors intend for the ! to be part of the dictionary type in their example? In any case, p. 57 is a curious place for an implicitly unwrapped optional to appear in the text as implicitly unwrapped optionals aren’t explained until p.95, so the example is confusing at best.

Remember that line in the book about “interesting times”? :slight_smile: This is simply a function of time and Swift working with a set of frameworks (Cocoa) built in Objective-C with very different type models.

The reason that that NSProcessInfo API was documented in the book as having an implicitly unwrapped optional type ("!") is that at that time, Apple had not yet audited that API for Swift – they didn’t know if it would ever return nil, so they left it like that.

In the latest batch of Xcode updates, however, they have now audited this API and many others and determined that it will not return nil, and so they removed the !.

Does that make sense?