Swift API Design Guidelines

Written by Yi Ding

Description: Swift 3 introduces new API Design Guidelines specifically crafted to the unique character of Swift for clear, concise code. This talk will explore the philosophy behind the Swift API Design Guidelines and their application throughout the Swift Standard Library and the Cocoa and Cocoa Touch APIs. See how this API transformation will affect your Swift code and learn how to ensure a smooth transition to Swift 3. Learn how Swift 3 imports Objective-C APIs and how to expose rich Swift interfaces for existing Objective-C libraries.

Swift API Design Guidelines



x.reverse()           // mutating

let y = x.reversed()  // non-mutating

“ing” rule

documentDirectory.appendPathComponent(".list")                        // mutating

let documentFile = documentDirectory.appendingPathComponent(".list")  // non-mutating

The Grand Renaming

extension MyController {
  func handleDrag(sender: UIControl, for event: UIEvent) { }  // handleDrag(sender:for:)
// Generated Objective-C
@interface MyController ()
- (void)handleDragWithSender:(UIControl *)sender for:(UIEvent *)event;

// After adding @objc(handleDrag:forEvent:)
// Generated Objective-C
@interface MyController ()
- (void)handleDrag:(UIControl *)sender forEvent:(UIEvent *)event;

Mapping Objective-C APIs into Swift

// Objective-C
typedef NSString * NSCalendarIdentifier NS_EXTENSIBLE_STRING_ENUM; 
NSCalendarIdentifier NSCalendarIdentifierGregorian;

// Generated Swift Interface
struct NSCalendarIdentifier : RawRepresentable {
    init(_ rawValue: String);
    var rawValue: String { get }
    static let gregorian: NSCalendarIdentifier

This note was originally published at github.com/antonio081014/WWDC_Learning_Review.

Missing anything? Corrections? Contributions are welcome 😃


Written by

Yi Ding

Yi Ding

Write the Code, Change the World.