
Therefore, the initialization above should handle the optional result directly: if let image = NSImage(contentsOfFile: "swift. The swift in depth book didn’t cover all types of initializer, so as result I don’t want to talk about those initializers too, because this article meant to be a synopsis of the book. When constructing an object using a failable initializer, the result is an optional that either contains the object (when the initialization succeeded) or contains nil (when the initialization failed). With Swift version 1.1, such failures can be reported using a failable initializer.

If the file does not exist or is unreadable for any reason, the initialization of the NSImage will fail. For example, initializing the object requires access to a resource, such as loading an image from a file: NSImage(contentsOfFile: "swift.png") Initialization is the process of providing initial values to each of the stored properties of a class or struct, establishing the invariants of the object. Swift version 1.1 is new in Xcode 6.1, and it introduces a new feature: failable initializers.


The place to initialize an instances constant attributes is in the. For the latest news, visit the Swift open source blog - Table of Contents - Preface - Chapter 1: Introducing Swift - Defining our goals for this book - Setting up the development environment. In Swift on the other hand, unwrapping nil, or downcasting to the wrong type.
