Sunday, October 8, 2017

Swift 4 NEW Tutorial-2 (Strings are Collections Again)

Prior to Swift 3, Strings are collection types, Now again in Swift 4 they are collections.

Swift 3 

-------------------------------------------------------------------------

var str = “Hello World!”
str.characters.count
str.characters.append(“appending….”)

-------------------------------------------------------------------------

Swift 4 

-------------------------------------------------------------------------

var str = “iOS Solves”
str.count        // 10
str.append(“ Blog”)      // iOS Solves Blog

-------------------------------------------------------------------------

Multiline strings :-

Swift 3

let name = “name1 \n name2 \n name3”

Output:

name1
name2
name3

Swift 4

Use 3 double quotes to specify it as a multiline string.

-------------------------------------------------------------------------

let name = “””
name1
name2
name3
“””

Output:

name1
name2
name3

-------------------------------------------------------------------------

Use spaces for indentation

-------------------------------------------------------------------------

let name = “””
  name1
  name2
  name3
“””

Output:

  name1
  name2
  name3

-------------------------------------------------------------------------

Hope this post is useful. Feel free to comment incase of any queries.


No comments:

Post a Comment