Differences in Swift 2.x and 3.x
Swift 2.x includes some features such as improved extensions and availability check protocols. Swift 3.x is the latest iteration of Apple’s programming language. As far as the language is concerned there are not many changes and they mostly affect more advanced features of Swift. However, the Cocoa Touch and Cocoa APIs have all changed dramatically. All for the better and are much simpler to understand. But any code written for Swift 2 will need to be migrated to Swift 3 and that probably affects every function. There is an automatic migrator included in Xcode 8.
1. API Design Guidelines:
The design of commonly-used libraries creates a major impact on the overall feel of programming language. To elevate the process of construction of Swift libraries, Swift 3 focuses on defining a set of API design guidelines and to apply those design guidelines consistently.
Major pieces that are used to define API Design Guidelines
a. Swift API Design Guidelines
b. Swift Standard Library
c. Imported Objective-C APIs
d. Swift Guidelines Checking
e. Swift 2 to Swift 3 Migrator
2. Omit Needless Words:
In this method with names that have self- contained words are now been removed.
For example, Swift 2.x has the coding like this:
And, Here is that same Code in Swift 3:
Swift 3 has made significantly shorter. This change particularly has a lot of effect on strings, which had repetition all over the place.
3. Modern GCD and core graphics :
Grand Central Dispatch is used for many threading tasks such as long calculations or to communicate with a server. In Swift 2.x the dispatch library was written in the C programming language and has always used a C style API.
Now in Swift 3.x API has now been re imagined in native Swift :
4. Capitalization in Enumeration Classes :
UpperCamelCase has been replaced to LowerCamelCase for enumeration classes
5. Function Types
When you use a function type as a parameter itself, you might write something like this:
With Swift 3 the correct way to define this function is:
6. Improvements to Tooling:
Swift 3 fixes bugs in compiler and IDE. Updates also enable it to improve the precision of error and warning messages. And the most amazing thing is, Swift is getting faster with each release in how it runs and compiles:
a. By improving string hashing there was a 3x speedup in dictionaries of strings
b. By moving objects from the heap to the stack there was a 24x speedup (in some cases)
c. The compiler now caches multiple files at once (when doing whole module optimization)
d. Code size optimization has reduced the compiled size of Swift code.
7. Swift importing of C functions:
Swift maintains compatibility with a number of C language types and features as a part of its interoperability with Objective-C. There are also ways available to work with common C constructs and patterns with Swift, in case your code requires it.
Same like, Swift 3 bring forth attributes for C functions which enable library authors to specify new and beautiful ways their code should be imported into Swift.
Here’s an example in Swift 2.x:
Now in Swift 3 the CGContext can be treated as an object that you can call methods on rather than repeating CGContext again and again :