In the book, in some places the property windowNibName of NSWindowController uses a string rather than an optional string:
For example, on page 138, it reads:
class MainWindowController: NSWindowController {
override var windowNibName: String {
return "MainWindowController"
}
Wheras a few pages later, on page 144, it reads:
class MainWindowController: NSWindowController {
override var windowNibName: String? {
return "MainWindowController"
}
From the documentation of NSWindowController, windowNibName should be an optional.
Could you explain if using a string intead of an optional there has any consequences ?
This looks like one of those bits that changed during the betas, thus the inconsistency. You are correct, they should all be String? (string optionals).
As far as consequences, there are two aspects to consider. First, the Swift language: based on experimentation, Swift doesn’t mind if you override a computed property or function returning an optional with a computed property or function returning a non-optional. This is consistent with Xcode not complaining about String vs String? in this case.
The other aspect is the context of Cocoa and Objective-C. In terms of the compiled code, without decompiling the code I am pretty certain that this has no impact. Because windowNibName is an override of an Objective-C property, the actual value returned will be an NSString *, which is the same type whether it is String or String? in Swift (all pointers can be thought of as optionals).
But again, yes, the book should have been more consistent. Thanks for pointing this out.