Renaming Objective-C APIs for Swift
Use the NS_SWIFT_NAME macro to customize API names for Swift.
Overview
If you want to import an Objective-C API into Swift with a different name, use the NS_SWIFT_NAME macro. The macro preserves the Objective-C name for use with Objective-C code, so the API has appropriate names in each language.
You apply the NS_SWIFT_NAME macro to an individual type, method, or function declaration in Objective-C. After applying the macro, the name you use in your Swift code will be what you’ve chosen by using the macro.
Rename APIs
The example below renames a class and one of its properties:
NS_SWIFT_NAME(Sandwich.Preferences)
@interface SandwichPreferences : NSObject
@property BOOL includesCrust NS_SWIFT_NAME(isCrusty);
@end
@interface Sandwich : NSObject
@endThe SandwichPreferences class and its includesCrust property are renamed to Sandwich.Preferences and isCrusty for Swift:
var preferences = Sandwich.Preferences()
preferences.isCrusty = trueYou use the NS_SWIFT_NAME macro as a prefix for classes and protocols. For all other kinds of declaration—such as properties, enumeration cases, and type aliases—you use the macro as a suffix. The following example uses the macro as a suffix to rename an enumeration:
typedef NS_ENUM(NSInteger, SandwichBreadType) {
brioche, pumpernickel, pretzel, focaccia
} NS_SWIFT_NAME(SandwichPreferences.BreadType);