神刀安全网

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(...)

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Preparing for 3.0 API Pruning

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮