Swift 4: The 6 best new features
Swift is a fantastic multi-platform programming language targeted for phones, desktops, servers, or anything else that runs code. The favorite app development language of Swift iOS developer brings a major update, and introduces a variety of features in Swift 4 that let us write simpler, safer code.
The new features introduced in Swift 4 include improved key paths for key-value coding, Encoding and decoding, Multi-line string literals, Strings are collections, improved dictionary functionality, One-sided ranges, and many other code enhancements.
1. Encoding and decoding
Swift is focused on bringing a codable protocol that lets you serialize and archive to and from any of the built-in data formats, and any formats. Even better, you can choose how you want the data to be serialized: you can use JSON or classic property list.
2. Multi-line string literals
Swift 4 has addressed the problems in adding new lines and using double quotes in string with a new Multi line string literal syntax. This lets you add line breaks freely and use three double quote marks and press return key. Thereafter start writing strings with any variables, line breaks and double quotes as you want.
3. Key-value coding
One of the important feature of Objective-C is its ability to grab root object name and drill down any property name dynamically rather than referencing an object key with a simple string literal. These references, key paths, enforce a compile-time check that a type contains the required key, thereby eliminating a common type of runtime error.
4. Dictionary enhancements
Swift 4 added a new robust functionality to Dictionary and Set. They are much more pleasant to use thanks to a few utility methods that have been added.
Swift 4 has a mapValues method on dictionaries to transform all values, and place them back into a dictionary using the original keys.
It’s now possible to access dictionaries and provide a default value when using the subscript syntax to avoid having to later unwrap an optional.
Finally, a new addition to Dictionary is a grouping initializer which allows you to create a new dictionary by grouping the sequences of an existing collection by whatever criteria you set.
Just like it was in earlier Swift version, strings are collections again. This means you no longer need to write character-by-character to perform string manipulation. This change was introduced as part of a broad set of amendments called the String Manifesto.
6. One-sided ranges
Swift got influenced from Python to use one side range where missing side will automatically be treated as start or end of sequence. We need not worry about potential breakage as it is not relevant to the existing code.
Now that you have taken a look at best six new features in Swift 4, you're probably eager to start using them, to help keep your codebase fresh and clean. We might see many more improvements to make their way in Swift 4.