Join Simon Allardice for an in-depth discussion in this video Working with NSError in Swift, part of Comparing Swift and Objective-C.
- We've already seen a few examples…of using Swift optionals to work with Objective-C methods…that might not return a value…but we can also call methods that need to do more than that…and they need to report error information.…Now traditional way to do this in Cocoa development…is to use an NSError object.…Here's an example,…I'm gonna begin with a Swift OS X Playground…and I'm going to use the NSFileManager class,…this is a class that we grab by calling…the defaultManager method of NSFileManager…and it allows us to do things like read files,…delete them, copy them, and move them.…
I've got a handle on this fileManager.…I can do things like moveItemAtPath,…I have options for copying an item at path,…I have options for reading the contents…at a particular path and so on.…I'm going to choose the option to moveItemAtPath.…Notice that this is a method defined to…return a Boolean, that will be important in just a minute.…I'll click return and start to fill this out.…I'm gonna fill this out providing two paths…that are intentionally not paths that exist on my machine…
Need a refresher on either language? Check out Swift Essential Training and Objective-C Essential Training.
- Exploring the interoperability of Swift and Objective-C
- Comparing language syntax
- Understanding how each language uses objects and methods
- Bridging arrays, dictionaries, and numbers
- Dealing with nil return types from Objective-C methods
- Working with NSError in Swift
- Working with Swift and Objective-C strings
- Adding Swift to an existing Objective-C project
- Inheriting between languages