It has been less than a year since Swift 3.0 has been released, but the work on the next version is all in progress. Many developers has already started testing the upcoming generation of the this programming language worldwide. In this tutorial, we will consider the important changes in Swift 4 and help you get assured in the advantages of the new edition.
The evolution of Swift started in 2014 when its first version was released during the Apple Worldwide Developers Conference (WWDC) in San Francisco. Since that time, it has got some significant updates that make this language easier to use and more optimized. According to the survey held in 2015 by Stack Overflow, Swift is the most loved programming language among all developers around the globe.
Swift releases:
- June 2, 2014 – Swift 1.0;
- June 8, 2015 – Swift 2.0;
- March 21, 2016 – Swift 2.2;
- September 13, 2015 – Swift 3.0.
The official release date of Swift 4.0 is still not defined. It is forecasted to be out of beta in the autumn of 2017. Although, it is already possible to try its beta version after performing the simple settings we will discuss in the next paragraph. Continue reading to find out how to perform migration from the previous edition of Swift to the new one.
Migrating to Swift 4
Before listing and analyzing all changes in this programming language, let’s consider how to update your current project in order to use a new edition. Does this task take much time and require installing additional Swift migration tools? The good news is that performing project migration to the new Swift version is now a much easier task in comparison with the previous release.
Xcode has an implemented instrument that will handle it for you. You can find this tool by following the path in Xcode 9: Edit/Convert/To Current Swift Syntax. Then you will need to select the elements to convert and choose an Objective-C inferencing option. To decrease a binary size of your project, select the “Minimize Inference (recommended)” option.
New Features in Swift 4
Taking into account that the latest version of Swift is still in beta, it is hardly possible to try all advantages and estimate all changes of this programming language before an official release. Furthermore, there still can remain some bugs or all fixes might not have been implemented yet. To stay tuned, it is important to keep an eye on new updates and testing news.
Decoding and Encoding
Due to API serialization and archival refreshing in the Foundation framework, new Swift eliminates the need to encode, parse, enumerate, or structure classes manually. So, JSON parsing is now available in Swift 4.0. Due to the new protocols called Decodable and Encodable, it is easy to conform classes by adding Codable to the inheritance list of classes. To encode a class instance, utilize JSONEncoder object in the following manner:
Moreover, if you need to store data in a plist file, you may utilize the new objects called PropertyListDecoder and PropertyListEncoder. It is also possible to create custom coders by conforming them to the corresponding protocols.
Key Paths
The usage of key paths was first implemented in the third version of Swift in order to ensure an easier object property referencing. Key paths allow to run a compile-time check whether a type has a required key. It lets avoid a frequent runtime error. Unfortunately, possibilities to use this solution were significantly limited in Swift 3.0 because of the tight connection to NSObject’s. Furthermore, key paths did not conform structs very well. That is why contributors have offered to use the “/” sign to specify a key path.
The object called nameKeyPath sets the path to the property called name. By changing variables from let to var for lunapps, it is possible to change properties using a corresponding subscript syntax.
Strings as Collections
It is hardly possible to call this change significant, but it makes the work with the programming language easier. A string in Swift 4 becomes a collection again. This update allows to perform the following actions with strings:
- Reversing strings;
- Looping over strings character-by-character;
- Using map() and flatMap() for strings;
- Etc.
This update was announced as a part of the String Manifesto amendment set.
One-sided ranges
The latest version of Swift will contain one-sided collection slicing as it exists in Python. It means that the missing side will be automatically set as a start or end for the collection. The good news is that it will not influence your current code because this is a new way to use an existing operator. Therefore, errors are impossible in this case.
This code will initiate printing “A, B, C” and then “d, e, f”. This update is part of the Swift Evolution proposal.
Mixing variables using swapAt
It is another small change in the new version of Swift but it deserves to be mentioned anyway. SwapAt is a function that allows to mix places of variables in a row:
We will get the following result: iOS, Android, Blackberry, Windows Phone.
Dictionary Enhancements
Swift 4 also allows to create dictionaries using array elements and mix them in a custom order. Here is an example:
We will get the following result: [2: “Android”, 3: “Windows Phone”, 1: “iOS”].
Setting a Default Value in the Array
The new changes in Swift allows to print default values of dictionaries if the needed value does not exist. In our previous example, we have not specified the fourth mobile operating platform. Therefore, the system will print the default value instead:
We will get the following result: “We have got only three of them.”.
Grouping array elements
There is a possibility to group array elements in different blocks by a custom order in Swift 4. The code below will allow to group our elements by a first letter in their names:
Adding a Multi-Line String Literal
In previous versions, you had to avoid using double quotes in strings and add new lines using /n. Fortunately, the time has changed and the only thing you need to do in order create a multiline string with variables or a simple text as in traditional editors is to add three double quotes marks “”” and press a return key. Then start typing as usual. The line will break and when you need to end a multi-line string, simply add closing three double quotes in a new line.
We will get the following result:
“Due to the new version of Swift, you need to use “/n” no more in order to type in a new line and you may use “double quotes” as you wish. Such a simple and useful change that will definitely help you out!”
This is only a small roadmap to Swift 4 and its new features that have been already implemented. It is hard to forecast other changes that can be added in the final version of this programming language. One way or another, its current updates prove their benefits and all mobile app developers worldwide are looking forward to new upgrades and proposals from contributors. Indeed, the existing changes makes the usage of Swift easier and each developer will appreciate them. We believe that our guide has managed to help you navigate through the latest news in the world of iOS mobile development.
In case you need to build a mobile solution using the new version of Swift, please feel free to contact our team at o.babentsov@lunapps.com. Our professional mobile developers have a deep expertise in creating native mobile applications in such domains as eCommerce, corporate communication, sport, auctions, games, and many more. You will get a highly optimized app with a beautiful and modern design with the newest technologies inside. Make your business successful with Lunapps!