New major release of Swift 3.0

Posted By : Aditya Kumar Sharma | 22-Dec-2016

 

 

 

 

Swift 3.0 is the major release of Swift which is available on Xcode 8. 

We can convert our existing project to Swift 2.3 or Swift 3 by Migration Assistant which is been included in xcode 8 by apple. You can do this by navigating to:

Edit > Convert > To Current Swift Syntax…

Proposals Implemented for Swift Evolution:

  • Establish consistent label behavior across all parameters including first labels

It is for normalizing the first parameter declaration in methods and functions. In this proposal, first parameter declarations will match the existing behavior of the second and later parameters. All parameters, regardless of position, will behave uniformly. This will create a simple, consistent approach to parameter declaration throughout the Swift programming language and bring method and function declarations in-sync with initializers, which already use this standard.

       NSJSONSerialization.JSONObjectWithData(data, options: []) //Swift 2
           JSONSerialization.jsonObject(with: data, options: [])   //Swift 3
 
           NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)   //Swift 2
           FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)   //Swift 3
 
           contentView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)   //Swift 2
           contentView.centerXAnchor.constraint(equalTo: view.centerXAnchor)   //Swift 3
 
           rollNoArray.removeAtIndex(3)   //Swift 2
           rollNoArray.remove(at: 3)   //Swift 3
 
           jpegImageData.writeToURL(fileURL, atomically: false)   //Swift 2
           jpegImageData.write(to: fileURL, options: [])   //Swift 3
 
           numberOfSectionsInTableView(tableView)   //Swift 2
           numberOfSections(in: tableView)   //Swift 3
           

Under these revised guidelines, first argument labels are encouraged for but are not limited to:

  • methods and functions where the first parameter of a method is defaulted
  • methods and functions where the first argument uses a prepositional phrase
  • methods and functions that implement factory methods
  • methods and functions where method arguments represent a split form of a single abstraction

 

  • Better Translation of Objective- C APIs Into Swift

Earlier iterations of Apple libraries method names indicate their return value and parameter names include the name of the type they expected. As Swift compiler is type checking it is less neccessary.

Some examples of how Objective-C APIs are evolved in native Swift:

          wishString.stringByAppendingString("Congratulations")    //Swift 2
           wishString.appending("Congratulations")    //Swift 3
 
           UIColor.cyanColor().colorWithAlphaComponent(0.5)    //Swift 2
           UIColor.cyan.withAlphaComponent(0.5)    //Swift 3
 
           NSBundle.mainBundle()    //Swift 2
           Bundle.main    //Swift 3
        
  • Modernize libdispatch naming convection

 The libdispatch library was written in the C programming language and has always used a C style API. Now it has evolved in native Swift:

         //----------------Swift 2----------------------------
           let queue = dispatch_queue_create("[perform task in background]", nil)
           dispatch_async(queue) {
              //Show on UI after task completion
              print("Task completed")
           }
 
           //----------------Swift 3----------------------------
           let queue = DispatchQueue(label: "[perform task in background]")
           queue.async {
               //Show on UI after task completion
               print("Task completed")
           }
          
  • Capitalization on Enumeration cases

Earlier we were using upperCamelCase for enumeration cases but now it has been changed to lowerCamelCase which is making them more consistent with all other properties.

       UIStatusBarStyle.LightContent    //Swift 2
           UIStatusBarStyle.lightContent    //Swift 3

           SequenceType.minElement()    //Swift 2
           SequenceType.min()    //Swift 3
           
  • Methods that Return or Modify

Now method naming are also getting consistent. Earlier we used to give name according to actions performed. But now the thumb rule is that if it includes a suffix like "-ed" or "-ing" then that methods should return a value. If it doesn't have any suffix then that method perform the action on referenced memory which is also known as modifying in place. Now we can use enumerate()/enumerated(), reverse()/reversed() and sort()/sorted().

          var dailyEarning = [500, 10000, 200] 
           dailyEarning .sort() // modified in place, value now [200, 500, 10000]
 
           for (index, dailyEarning ) in dailyEarnings.enumerated() { 
             print("\(index). \(dailyEarning )") // 1. 200 \n 2. 500 \n 3. 10000
           }
           
  • Miscellaneous Odds and Evens
  • Now we can use "pi" with the tpye we intend to use it: Float.pi, CGFloat.pi
  • The NS prefix has been removed, now we can use Calendar, Date instead of NSCalendar and NSDate.

 

Improvements to Tool

 

Swift 3 has fixed bugs of compiler and IDE features. It has also improved the precision of errors and warnings messages. And with this release swift is getting faster in running and compling the code.

  • By improving string hashing there was a 3x speedup in dictionaries of strings
  • By moving objects from the heap to the stack there was a 24x speedup (in some cases)
  • The compiler now caches multiple files at once (when doing whole module optimization)

 

Thanks

About Author

Author Image
Aditya Kumar Sharma

Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.

Request for Proposal

Name is required

Comment is required

Sending message..