Skip to content

Instantly share code, notes, and snippets.

@RishabhTayal
Last active April 7, 2017 17:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RishabhTayal/e34f97733e6bfe2e08ad2122dab83437 to your computer and use it in GitHub Desktop.
Save RishabhTayal/e34f97733e6bfe2e08ad2122dab83437 to your computer and use it in GitHub Desktop.

Swift 3 Changes

1. Simpler APIs, Omit unneccessary words.

helloString.stringByAppendingString("world")
helloString.appending("world")

let blue = UIColor.blueColor()
let blue = UIColor.blue

NSBundle.mainBundle()
Bundle.main
 
numbers.maxElement()
numbers.max()
 
animals.insert("Koala", atIndex: 0)
animals.insert("Koala", at: 0)

2. GCD and Core Graphics.

let queue = dispatch_queue_create("com.test.myqueue", nil)
dispatch_async(queue) {
    print("Hello World")
}

DispatchQueue(label: "com.test.myqueue").async {
  print("Hello World")
}

CGContextSetLineWidth(context, strokeWidth)
context.setLineWidth(strokeWidth)

CGContextDrawPath(context, kCGPathStroke)
context.drawPath(using: .stroke)

3. Capitalization on Enumeration Cases

UIStatusBarStyle.LightContent
UIStatusBarStyle.lightContent

doneButton.setTitleColor(.LightGrayColor(), for: .Disabled)
doneButton.setTitleColor(.lightGray, for: .disabled)

4. Function Changes

override func numberOfSectionsInTableView(tableView: UITableView) -> Int
override func numberOfSections(in tableView: UITableView) -> Int


dismissViewControllerAnimated(true, completion: nil)
dismiss(animated: true)

override func viewWillAppear(animated: Bool)
override func viewWillAppear(_ animated: Bool)

5. Completion Block Use @escaping before the completion blocks.

6. Use _ where ever required in the function declaration

func tappedOnView(view: UIView) {
        view.endEditing(true)
}
self.tappedOnView(view: view)

func tappedOnView(_ view: UIView) {
        view.endEditing(true)
}
self.tappedOnView(view)

func tapped(on view: UIView) {
}
self.tapped(on: view)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment