Swift 4.0 is a major new release for everyone's favorite app development language, and introduces a variety of features that let us write simpler, safer code. Swift 4 release is designed around two primary goals: to provide source stability for Swift 3 code and to provide ABI stability for the Swift standard library
WARNING: Swift 4 is still under active development. Please keep in mind that more features are likely ship in the months before final release.
Multi-Line String Literals — SE-0168
Adding new lines using \n and escaping double quotes in string is not always pleasant task.
let multiLineString = """
This is one of the best feature add in Swift 4
It let’s you write “Pradeep Chauhan” new lines without need of “\n”
"""
print(multiLineString)
Smart KeyPaths: Key-Value Coding — SE-0161
You can grab root object name and drill down upto any property name to get its value. Key path starts with \ operator.
struct Address {
var city:String
var state:String
}
struct User {
var name: String
var addresses: [Address]
var primaryAddress: Address {
return addresses.first!
}
}
Now If we have friend object and want to get the value then following key path syntax should be used.
print(user[keyPath:\User.primaryAddress.state])
Swift Archival & Serialization — SE-0166
Good News! Swift 4 comes with built in encoders and decoders for JSON. JSON <-> Model conversion is comes built in ( Although you can customize that behavior if required ).
Swift 4 introduces a new Codable
protocol that lets you serialize and deserialize custom data types without writing any special code – and without having to worry about losing your value types. Even better, you can choose how you want the data to be serialized: you can use classic property list format or even JSON.
struct Address:Codable {
var city:String
var state:String
}
struct User: Codable {
var name: String
var address: Address
}
Encoding ( Model -> JSON )
Let's fill out some details
let address = Address(city: "Udaipur", state: "Rajasthan")
let user = User(name: "pradeep Chauhan", address: address)
let encoder = JSONEncoder() // Define JSONEncoder
if let encoded = try? encoder.encode(user) {
if let json = String(data: encoded, encoding: .utf8) {
print(json)
}
}
Decoding ( JSON -> Model )
let decoder = JSONDecoder()
if let decoded = try? decoder.decode(User.self, from: encoded) {
print(decoded.name)
print(decoded.address)
}
Strings are collections again
— SE-0163
This is a small change, but one guaranteed to make a lot of people happy: strings are collections again. This means you can reverse them, loop over them character-by-character, map()
and flatMap()
them, and more. For example:
let str = "What's new in swift 4"
for ch in str {
print(ch)
}
MutableCollection.swapAt— SE-0173
New swap function now takes indices of elements which are to be swapped.
var list = ["Pradeep","Chauhan","iOS","Developer"]
list.swapAt(1,2)
Output.
["Pradeep","iOS","Chauhan","Developer"]
One-sided Ranges— SE-0172
Now we can use one side range where missing side will automatically treated as start or end of sequence.
let list = ["Pradeep","Chauhan","iOS","Developer"]
let firstTwo = names[..<2]
let lastThree = names[1…]
print(firstTwo)
print(lastThree)
Which will produce following output.
["Pradeep", "Chauhan"] // [..<2] => [0,1]
["Chauhan","iOS","Developer"] // list[2…] => [1,2,3]
Dictionary and Set enhancements— SE-0165
- you can create a dictionary using array elements.
let users = ["Ajay", "Ratish", "Vishal"] let usersDictionary = Dictionary(uniqueKeysWithValues: zip(1…, users)) print(usersDictionary)
Which will produce following output.
["0":"Ajay", "1":"Ratish", "2":"Vishal"]
Comments
puneet
Well Done! Pradeep :)
SNEHAL Rokade
Nice article ..
Nikesh pathak
Nice post, keep it up pradeep
SRISHTY KOTIA
👍😊
Ganeshakumar K
Good one Pradeep, Nice example too... Keep it up.
Dnyaneshwar Shinde
Nice article ...