What’s new in Strings in Swift 4?

The Swift 4 which is default in Xcode 9. So you have to download Xcode 9. if you have Xcode 8.3 then you can also installed Swift 4 toolchain.


Multi-line string literals :  You can define the multi-line string using three double-quotes ("""). see the below code.
let multilineString = """
        I am Kirit Modi.
        Working as iOS Developer.
        Having 5+ Year experience iOS development.
        Currently working also in React Native.
    """
print(multilineString)



Escaping newlines in string literals: 
Add the backslash ( \ ) at the end of the line to escape new lines in the multi-line string literals.
let escapedNewline = """
    I am Kirit Modi, \
    Working as iOS Developer.
    """
print(escapedNewline)
String is a Collection :  The biggest change is that string is a Collection. Swift 1 also is a collection. see the code of for-in loop to get characters from string.

let greeting = "Kirit, M😜d!"
greeting.count
for char in greeting {
    print(char)
}

Unicode 9: The Swift 4 supports unicode 9. now the issue of the number of count of unicode is also resolve in swift 4.0.
"👧🏽".count // person + skin tone; in Swift 3: 2 Swift 4 : 1
"👨‍👩‍👧‍👦".count // family with four members; in Swift 3: 4 Swift 4 : 1
"👱🏾\u{200D}👩🏽\u{200D}👧🏿\u{200D}👦🏻".count // family + skin tones; in Swift 3: 8 Swift 4 : 1
"👩🏻‍🚒".count // person + skin tone + profession; in Swift 3: 3 Swift 4 : 1
"🇨🇺🇬🇫🇱🇨".count // multiple flags; in Swift 3: 1 Swift 4 : 3
Character.unicodeScalars property:
The You can access the code point of character directly without to convert it to a string.

let c: Character = "🇪🇺"
Array(c.unicodeScalars)
let d: Character = "🇬🇫"
Array(d.unicodeScalars)

String Slice: 
There is a new way to Splice the original string. you can see the below code to Slice the string.
let greeting = "Kirit, M😜d!"
let comma = greeting.index(of: ",")!
let substring = greeting[..<comma]

Please add comment to improve or any missing in the article. 


Thanks. 

What’s new in Strings in Swift 4? What’s new in Strings in Swift 4? Reviewed by Kirit Modi on 03:19:00 Rating: 5

No comments:

Powered by Blogger.