Preparing for 3.0 API Pruning

Just another reminder this morning, with a quote from Chris Lattner that Swift 3 is barreling towards us with a huge impact on Cocoa and CocoaTouch familiarity:

[I]n Swift 3, “.blackColor()” becomes “.black()”.

This change is covered in SE-0005, the proposal about translating Objective-C APIs into Swift. The rule in question (Pruning #3) prunes the word color based on the match between the tail of the property name and the property type name:

class func darkGrayColor() -> UIColor
// so
foregroundColor = .darkGrayColor()
// becomes
foregroundColor = .darkGray()

Here’s a quick summary of the pruning rules. Keep in mind that if following the rules produces a no-go result (an empty selector, a Swift keyword, etc), that rule is skipped.

Pruning #1: Chop type names off the front of members that preserve the same type:

let c = myColor.colorWithAlphaComponent(0.5)
// becomes
let c = myColor.withAlphaComponent(0.5)

Pruning #2: If the type name is followed with by and is followed by a present participle gerund chop the “by” off too.

let img = myImage.imageByApplyingOrientation(o)
// becomes
let img = myImage.applyingOrientation(o)

Pruning #3. Prune the type name at the tail of a selector when it matches the type name under the following circumstances:

From the tail of: Prune a match for:
a selector piece that introduces a parameter the parameter type name
the name of a property the property type name
the name of a zero-argument method the return type name
documentForURL(_ url: NSURL)
var parentContext: NSManagedObjectContext?
class func darkGrayColor() -> UIColor
// becomes
documentFor(_ url: NSURL)
var parent: NSManagedObjectContext?
class func darkGray() -> UIColor

Pruning #4: Prune types after verbs in method names:

myVC.dismissViewControllerAnimated(...)
// becomes
myVC.dismissAnimated(...)

4 Comments

  • I don’t know if it’s been discussed but I’d love to see these methods get default values so, for example, dismissViewControllerAnimated(flag: true, completion: nil) becomes dismiss(animated:true, completion: nil) becomes dismiss() with default values for animated and completion.

    Similarly, button.title(forState:.Normal) for normal state is just button.title() and button.setTitle(“New title”)

    Best,

    Daniel

  • […] Swift will do it again. Be prepared for 3.0 API Pruning. Removing more duplicated staff from the API, like: .blackColor() will be .black(), somethingForURL(url: NSURL) will be somethingFor(url: NSURL). Bright side is that it’s really annoying if you type a sequence of calls to the same class (e.g. UIColor.blackColor.colorWithAlphaComponent(…)), but it makes code less readable for Objective-C developers and might cause name ambiguity. […]

  • […] If you read only one blog about Swift programming, make it Erica Sadun’s. She’s forgotten more about iOS development that I’ll ever learn. In a recent post, she tells us all about getting ready for the API pruning that’ll happen when Swift 3.0 — which is coming soon — is released… […]

  • […] Sadun, 原文链接 […]