Working with Foundation Types
Use bridged Foundation types in your Swift codebase to work with dates, times, and other values.
Overview
When importing the Foundation framework, the Swift overlay provides value types for many bridged reference types. Many other types are renamed or nested to clarify relationships.
Prefer Swift Value Types to Bridged Objective-C Reference Types
The value types in the table below have the same functionality as their corresponding reference types. Class clusters that include immutable and mutable subclasses, like NSArray and NSMutableArray, are bridged to a single value type.
Swift value type | Objective-C reference type |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Swift numeric types ( |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
When Swift code imports Objective-C APIs, the importer replaces Foundation reference types with their corresponding value types. For this reason, you should almost never need to use a bridged reference type directly in your own code.
If you need the reference semantics that come with the Foundation reference type, you can access it with its original NS class name prefix. Cast between a Swift value type and its corresponding reference type by using the as keyword.
let dataValue = Data(base64Encoded: myString)
let dataReference = dataValue as NSData?Note Renamed Reference Types
For Foundation types that aren’t bridged to value types, the Swift overlay renames classes and protocols, as well as related enumerations and constants. These types and protocols drop their NS prefix, with the following exceptions:
Classes specific to Objective-C or inherently tied to the Objective-C runtime, like
NSObject,NSAutoreleasePool,NSException, andNSProxyPlatform-specific classes, like
NSBackgroundActivity,NSUserNotification, andNSXPCConnection
Foundation classes often declare enumeration or constant types. When importing these types, Swift moves them to be nested types of their related types. For example, the NSJSONReadingOptions option set is imported as JSONSerialization.ReadingOptions.