Swift programming language is developed by Apple Inc for various operating system iOS, watchOS, macOS, Linux, and tvOS with easiness and advanced features to work on Swift. It is safe, having a modern approach, powerful, have many other features with whom you must be familiar with but In swift 4 there are many new features which we need to learn. So in this blog, I will provide you 6 New iOS Swift-4 Features for Better Programming Experience.
![](http://findnerd.s3.amazonaws.com/imagedata/8211/8211.jpg)
1. Taking string as collection
In swift 4 string is treated as a collection. In earlier version whenever we have to check the character count of string we use "string.characters.count", but now we no need to use character word, we can directly find count by using below code.
let message = "Message!"
message.count
2. One side range
Using one side range feature, the missing side will be treated as start or end of the sequence and this will be done automatically.
let names = [aaa, bbb, ccc, ddd, eee]
let firstTwoString = names[..<2]
let lastThreeString = names[2]
print(firstTwoString)
print(lastThreeString)
Which will produce following output:
["aaa", "bbb"] // [..<2] => [0,1]
["ccc", "ddd", "eee"] // names[2] => [2,3,4]
3. Create dictionary by using array
In swift 4 we can create a dictionary by using an array and for this, we have to use “zip” as done below.
let friends = [Hi, Hello, Bye] // dictionary
let friendsDictionary = Dictionary(uniqueKeysWithValues: zip(1, friends))
print(friendsDictionary)
Which will produce following output:
[2: hello, 3: Bye, 1: Hi]
4. Default value in dictionary
In swift 4 if the value or key is not present we can give default values also as done below.
let friends = [one, two, three]
let friendsDictionary = Dictionary(uniqueKeysWithValues: zip(1, friends))
print(friendsDictionary[10, default: No Friends Exist yet!]) // if there is no value for key 10 found then it will print default value.
5. Separate array elements
It is possible to place array elements in different buckets in swift 4.
Below code will separate the array elements on the basis of their first character.
let mates = [Akshay, Anil, Vishal, Vinay, Ankit]
let buckets = Dictionary(grouping: mates, by: { $0.first! })
The output will be ->
[A: [Akshay, Anil, Ankit], V: [Vishal, Vinay]]
6. Multi-Line String Literal
In swift 4 for Multi-Line string, we don’t need to use “\n”. Now for Multi-Line, we will use (“””) for opening and (“””) for closing or we can say we use open or close delimiters. Make sure to start your string from the new line and close the multiline by using three double-quotes in the new line. It shouldn't be like ->
let multiline = “””Line One
Line Two
Line Three“””
let multiline =
""" // used to start the multiline
Line One // start string from new line
Line Two
Line Three
""" // used to end the multiline, this closing quotes has to be in new line.
print(Multi-Line is : \(multiline))
We will get below output->
Multi-Line is : Line One
Line Two
Line Three
These are all the new Important Features, if you have any inputs, please feel free to share with us in the comment section below.
0 Comment(s)