Join Simon Allardice for an in-depth discussion in this video Bridging arrays, dictionaries, and numbers, part of Comparing Swift and Objective-C.
- View Offline
- As with Swift strings being bridged over to NSString,…Swift arrays and dictionaries are also bridged…to their corresponding Objective-C classes,…NSArray, and NSDictionary.…While this is incredibly useful and convenient…for using Objective-C APIs,…there are some fundamental differences in behavior.…The basic distinction is that Swift arrays…and dictionaries are typed.…In Objective-C NSArrays and NSDictionaries are not.…You can have any kind of object at any position.…To review the example that I showed…in the previous video about [AnyObject],…if you call an Objective-C method…that returns an array, that's an NSArray,…and what you'll automatically get in Swift,…is a Swift array of any object.…
It may be totally true the the Objective-C class…does return an NSArray that is just date objects,…or just string objects, or just UI controls,…or in this case, just AV player item objects.…But because NSArrays technically support…any kind of object at any position,…you will still get a Swift array of type [AnyObject].…Again, if you know for sure that what you're getting back…
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