Thursday, November 17, 2016

Swift 3 Updates

Hey there,

I just went through the list changes in Swift3 and i'll log an abstract of those changes here.



1.) First parameter label: Now, all the function params have labels.

2.) Omitted needless words:
Some Examples from Swift2 -> Swift3:
  • UIColor.blueColor() -> UIColor.blue,
  • numbers.minElement() -> numbers.min(),
  • "Hi there".lowercaseString -> "Hi there".lowercased,
  • Similarly 'uppercased' for 'uppercaseString'


3.) UpperCamelCase has been replaced with lowerCamelCase for enums and properties:
  • NSURLRequest(URL: someURL) -> URLRequest(url: someURL)
  • NSTextAlignment.Left -> NSTextAlignment.left
  • UIInterfaceOrientationMask.Portrait -> UIInterfaceOrientationMask.portrait

4.) GCD and CoreGraphics API's are changed from C style to Swift:

GCD:
In Swift2:
"dispatch_after(dispatch_time, dispatch_get_main-queue(), {})"

In Swift3:
"DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {}"

CoreGraphics:
In Swift2:
guard let context: CGContext = UIGraphicsGetCurrentContext() else { return }
CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor)
CGContextSetLineWidth(context, 2)
CGContextDrawPath(context, .Stroke)

In Swift3:
guard let context: CGContext = UIGraphicsGetCurrentContext() else { return }
context.strokeColor = UIColor.red().cgColor
context.lineWidth = 2
context.drawPath(mode: .Stroke)


5.) Avoid calling with class names: 
'Self' to call class methods & 'self' to call instance methods. (Previously we'd to use class name to call class methods)

6.) Class prefixes removed:
NS removed from Timer, UserDefaults, FileManager, Data, Date, Calendar, URL, URLRequest, UUID, NotificationCenter and more

7.Swift Package Manager: Official dependency manager

8.) Inline Sequences:
In Swift2: sequence(first: someView, next: { $0.superview }) { // someView, someView.superview, someView.superview.superview,... } In Swift3: for x in sequence(first: 0.1, next: { $0 * 2 }) .prefix(while: { $0 < 4 }) { // 0.1, 0.2, 0.4, 0.8, 1.6, 3.2 }

No comments: