History of changing Swift

Tram Ho

It can be said that for every iOS developer, the Swift language was born to mark a revolutionary step when replacing the Objective C language for too long. Experiencing the changes from 1.2 to 2.0 is already very “scary”, then you will be surprised with version 3.0 and then the upgrade to version Swift4, Swift5. However, Swift 3.0 is still a memorable milestone, the purpose of the article is for readers to understand the changes with the Swift language through versions. In Part 1, we will learn about the change from the old version of Swift to Swift 3 as a revolution. So what are those changes? What’s new in Swift 3? Let’s find out together!

All function parameters have a label (name).

In Swift 2.0, we have seen a change in the way we call functions and methods. And with this 3.0 version, we will see this change again, to a higher level. Since Swift 2.x and earlier, the method name did not require a label in the first parameter, so the name of the first parameter is always attached to the method name. For example:

Swift 3 will default to a label request for the parameter (you can adjust it to change it), meaning that the method name will no longer specify the parameters. In fact, the end of the function name will often turn into the name of the first parameter.

To illustrate, here is a code in Swift 2.2 with the corresponding example in Swift 3:

These are the methods that you have called, and also the domino effect for the method called: When you connect to some framework like UIKit, the methods will follow the same “no parameter first name” rule. previous versions (this applies to Swift 3 as well).

Here are some typical examples in Swift 2.2:

With Swift 3, all need underscore (_) before the first parameter, to signal that the caller (Objective-C code) will not use parameter labels:

Remove unnecessary words

When Swift became open source last December, Swift’s guildlines API foresaw even a change in version 3 with three words: “omit needless words” (eliminating unnecessary words). And now that we’ve seen this change in Swift 3, the method name will remove the obvious words.

lowerCamelCase replaces UpperCamelCase with enum and property

Although not very relevant (syntactically), but using capital letters in class names, structs, properties, enums, etc., we often follow a convention: classes, structs and enum use UpperCamelCase (MyStruct, WeatherType.Cloudy), property and parameter names use lowerCamelCase (emailAddress, requestString).

However, there are some exceptions, and these will no longer exist in Swift 3: properties and parameters that begin with an abbreviation will now use lowerCamelCase.

This change will increase more consistency: all properties and parameters will start with lowercase letters, and there will be no exceptions.

Also, the enum case has a change, from UpperCamelCase to lowerCamelCase. In my opinion, this change is quite correct: enum is a data type (like struct), but enum value is closer to property. In other words, all Apple enum you use will now be lowercase:

Swifty importing of C functions

Swift 3 also introduces attributes for functions in the C language, allowing library creators to specify new ways to enter code into Swift quickly and accurately. For example, all functions that begin with “CGContext” will be mapped to properties and methods on a CGContext object.

To see more clearly, the following is an example in Swift 2.2:

In Swift 3, CGContext can be considered as an object that you can call method, without having to repeat CGContext continuously. So, we can rewrite the code above as follows:

Conclude

Swift is still being developed and further improved by Apple. In this part 1 article, we have grasped the changes of Swift3 compared to the previous verison. In the next part 2, we will learn about new things on Swift4 and Swift5

Thank you for taking the time to post!

Source:

https://www.hackingwithswift.com/swift3

Share the news now

Source : Viblo