Working with Foundation Types

In Objective-C, a number of familiar types are implemented as classes as part of the Foundation framework: NSString, NSNumber, NSArray, NSDictionary, and NSSet. Because Cocoa was built for Objective-C, you will often run into these classes when working with the Cocoa APIs. The good news is that Apple has made transitioning between the Swift and Foundation (Objective-C) counterparts relatively painless. They are toll-free bridged, meaning that there is minimal computational cost in converting between the two.

Basic bridging

Swift types are automatically bridged to their Foundation counterparts:

l​e​t​ ​s​t​r​i​n​g​ ​=​ ​"​H​o​w​d​y​"​ l​e​t​ ​o​b​j​c​S​t​r​i​n​g​:​ ​N​S​S​t​r​i​n​g​ ​=​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.