Overview
In Part 2 of my Blog, you will learn more about some new features released in WWDC 2017 for iOS where I will cover topics like how to use and implement search controller within navigation bar and what’s new in Swift 4 (Swift 4 is in beta so still there’s a lot to come). To learn about Drag and Drop, Swipes Actions and other updates, please refer my previous blog Things to Keep in Mind for iOS 11 App Development – Part 1
Search Controller
Navigation bar now also provides support for searchController.
How to implement in your application?
– Declare Variable.
var searchController : UISearchController!
– In viewDidLoad.
self.searchController = UISearchController(searchResultsController: nil) self.navigationItem.searchController = searchController
What’s new in Swift 4?
Multiline String
- In previous versions of swift, if you want to move text to a new line developer needs to add `\n’ to break line but now it is no more needed.
- Now developer can write multiple lines of string by using “”” (three double quotation). Write whole string between “”” and you have your string with multiple lines without using “\n”.
Implementation
let multiLine = """ Yudiz refers to the spirit of youth. The Google ranking recognizes us as a top mobile app development company, globally. Example of “Double Quotes” without any escaping. """ print("MultiLineString: - \(multiLine)")
Output
MultiLineString: –
Yudiz refers to the spirit of youth.
The Google ranking recognizes us as a top mobile app development company, globally.
Example of “Double Quotes” without any escaping.
String Character
- Until now, if you want to perform string manipulation, we wrote string.characters.xxx. But now it’s no longer required.
Implementation
let stringCharacters = "HelloWorld!" for character in stringCharacters { print("Individual Character: - \(character)") }
Output
Individual Character: –
H
e
l
l
o
W
o
r
l
d
!
Swap Array Elements
- There is a small change that I think you must be aware of it, as you will need this in your app at any point.
- Swap function takes the two indexes which are to be swapped.
Implementation
var arrNames = ["Kevin","Nishal","Vishal","Ravi","Chirag","Shivam","Mahavir"] arrNames.swapAt(1, 2) print("Swap: - \(arrNames)")
Output
Swap: –
[“Kevin”, “Vishal”, “Nishal”, “Ravi”, “Chirag”, “Shivam”, “Mahavir”]
One Side Range
- Side Range returns number of elements (based on your condition) from array without using loop.
Implementation
var arrNames = ["Kevin","Nishal","Vishal","Ravi","Chirag","Shivam","Mahavir"] let lessThan = arrNames[..<3] print("Less than: - \(lessThan)") let greaterThan = arrNames[3...] print("Greater than: - \(greaterThan)")
Output
Less than: –
[“Kevin”, “Nishal”, “Vishal”]
Greater than: –
[“Ravi”, “Chirag”, “Shivam”, “Mahavir”]
Create Dictionary using single array
Implementation
let arrNames = ["Kevin","Nishal","Vishal","Ravi","Chirag","Shivam","Mahavir"] let createDictionary = Dictionary(uniqueKeysWithValues: zip(1..., arrNames)) print("Created Dictionary: - \(createDictionary)")
Output
Created Dictionary: –
[2: “Nishal”, 4: “Ravi”, 5: “Chirag”, 6: “Shivam”, 7: “Mahavir”, 3: “Vishal”, 1: “Kevin”]
Grouping elements of array with key
- Now grouping dictionary is easier than ever.
- This feature will be very useful for ‘contacts’ type of app.
Implementation
let arrNames = ["Kevin","Krishna","Nishal","Vishal","Vipul","Ravi","Chirag","Shivam","Mahavir"] let groupElements = Dictionary(grouping: arrNames, by: { $0.first! }) print("Group Name: - \(groupElements)")
Output
Group Name: –
[“V”: [“Vishal”, “Vipul”],
“N”: [“Nishal”],
“C”: [“Chirag”],
“R”: [“Ravi”],
“K”: [“Kevin”, “Krishna”],
“M”: [“Mahavir”],
“S”: [“Shivam”]]
JSON Encoder and Decoder
- One of the best feature in Swift 4.
- In order to use this feature, custom class/struct/enum must be Codable.
- You can customize behaviour if required.
Implementation
//Define Models struct Address: Codable { var state : String var city : String var zipCode : String } class Person: Codable { var name : String var address : Address init(name: String,address: Address) { self.name = name self.address = address } } //Fill data let address = Address(state: "Gujarat", city: "Ahmedabad", zipCode: "380061") let person = Person(name: "Kevin", address: address) //Encoding and Decoding code let encoder = JSONEncoder() if let encode = try? encoder.encode(person) { if let json = String(data: encode, encoding: .utf8) { print("Json Encoded Data: - \(json)") } // Decode let decoder = JSONDecoder() if let decode = try? decoder.decode(Person.self, from: encode){ print("Decoded Name: - \(decode.name)") print("Decoded Address: - \(decode.address)") } }
Output
Json Encoded Data: –
{
“name”:”Kevin”,
“address”:{
“state”:”Gujarat”,
“city”:”Ahmedabad”,
“zipCode”:”380061″
}
}
Decoded Name: –
Kevin
Decoded Address: –
Address(state: “Gujarat”, city: “Ahmedabad”, zipCode: “380061”)
Conclusion
So that’s it for blog part 2, lets hope for more this type of features and enhancements in final version.
Happy Coding !!