3
Strings and Characters

WHAT YOU WILL LEARN IN THIS CHAPTER:                            

  • How to define a string literal
  • The copy behavior of strings
  • The difference between characters and strings
  • How to use the various special string characters
  • How to use Unicode characters in Swift
  • How to use the various common string functions
  • How type conversion works for strings
  • How the String type interoperates with the NSString class

In the previous chapter, you learned about the various basic data types supported in Swift as well as some of the new features it introduces—tuples, optional types, and enhanced enumerations. In this chapter, you will learn how strings are represented in Swift using the String type and how it is bridged seamlessly with the NSString class found in the Foundation framework in Objective-C. In particular, because Swift supports Unicode natively, there are some areas that you need to pay attention to when dealing with strings. All of these are discussed in this chapter.

STRINGS

In Swift, a string literal is a sequence of characters enclosed by a pair of double quotes (""). The following code snippet shows a string literal assigned to a constant and another to a variable:

let str1 = "This is a string in Swift"        //---str1 is a constant---
var str2 = "This is another string in Swift"  //---str2 is a variable---

Because the compiler uses type inference, there is no need to specify the type of constant and variable that is being assigned the string. However, if you wish, you could still specify the String type explicitly:

var str3:String = "This is yet another string in Swift"

To assign an empty string to a variable, you can simply use a pair of empty double quotes, or call the initializer of the String type, like this:

var str4 = ""
var str5 = String()

The preceding statements initialize both str4 and str5 to contain an empty string. To check whether a variable contains an empty string, use the isEmpty() method of the String type:

if str4.isEmpty {
   println("Empty string")
}

Mutability of Strings

The mutability of a string means whether it can be modified after it has been assigned to a variable. In Swift, a string’s mutability is dependent on whether it is assigned to a constant or a variable.

A string that is assigned to a variable is mutable, as the following shows:

var myName = "Wei-Meng"
myName += " Lee"
println(myName) //---Wei-Meng Lee---

A string that is assigned to a constant is immutable (i.e., not mutable—its value cannot be changed):

let yourName = "Joe"
yourName += "Sim"     //---error---
yourName = "James"    //---error---

Strings as Value Types

In Swift, String is a value type. This means that when you assign a string to another variable, or pass a string into a function, a copy of the string is always created. Consider the following code snippet:

var originalStr = "This is the original"
var copyStr = originalStr

In the preceding example, originalStr is initialized with a string literal and then assigned to copyStr. A copy of the string literal is copied and assigned to copyStr, as shown in Figure 3.1.

images

Figure 3.1

If you output the values of both variables, you can see that both output the same string literal:

println(originalStr)  //---This is the original---
println(copyStr)      //---This is the original---

Now let’s make a change to the copyStr variable by assigning it another string literal:

copyStr = "This is the copy!"

What happened here is that copyStr is now assigned another string, as shown in Figure 3.2.

images

Figure 3.2

To prove this, output the values of both variables:

println(originalStr)
println(copyStr)

The preceding code snippet would output the following:

This is the original
This is the copy!

Characters

As mentioned earlier, in Swift a string is made up of characters. You can iterate through a string and extract each character using the For-In loop. The following code snippet shows an example:

var helloWorld = "Hello, World!"
for c in helloWorld {
    println(c)
}

The preceding statements output the following:

H
e
l
l
o
,
W
o
r
l
d
!

The For-In loop works with Unicode characters as well:

var hello = "您好"  //---hello contains two Chinese characters---
for c in hello {
    println(c)
}

The preceding code snippet outputs the following:

您
好

By default, using type inference the compiler will always use the String type for a character enclosed with double quotes. For example, in the following statement, euro is inferred to be of String type:

var euro = "€"

However, if you want euro to be the Character type, you have to explicitly specify the Character type:

var euro:Character = "€"

To append a string to a character, you need to convert the character to a string, as the following shows:

var euro:Character = "€"
var price = euro + "2500"  //---€2500---
euro += "2500"     //---error---

Concatenating Strings

In Swift, you can concatenate strings using the + operator:

var hello = "Hello"
var comma = ","
var world = "World"
var exclamation = "!"
var space = " "
var combinedStr = hello + comma + space + world + exclamation
println(combinedStr) //---Hello, World!---

You can also use the addition assignment operator (+=) to append a string to another string:

var hello = "Hello"
hello += ", World!"
println(hello) //---Hello, World!"

In the preceding examples, you are concatenating variables of the same type, which in this case is String. If you want to concatenate a String variable with variables of other types, there are a few things you need to note. Consider the following statements:

var euro:Character = "€"
var amount = 500

Here, euro is of type Character, and amount is of type Int. The easiest way to combine the two variables into a string is to use string interpolation. String interpolation has the following syntax:

(variable_name)

The following statement uses string interpolation to combine the value of euro and amount into a single string:

var amountStr1 = "(euro)(amount)"
println(amountStr1) //---€500---

If you try to concatenate a string together with a numeric value (such as Double or Int), you will get an error:

var amountStr2 = "(euro)" + amount //---error---

Instead, you should explicitly convert the numeric value to a string using the String() initializer:

var amountStr2 = "(euro)" + String(amount)

Likewise, if you try to concatenate a Character type and an Int type, you will get a compiler error:

var amountStr3 = euro + amount //---error---

As usual, you should convert both types to String before concatenating them:

var amountStr3 = String(euro) + String(amount)

Special Characters

String literals can contain one or more characters that have a special meaning in Swift.

If you want to represent the double quote (") within a string, prefix the double quote with a backslash ():

var quotation =
    "Albert Einstein: "A person who never made a mistake never tried
    anything new""
println(quotation)

The preceding statement outputs the following:

Albert Einstein: "A person who never made a mistake never tried anything
new"

If you want to represent the single quote (') within a string, simply include it in the string:

var str = "'A' for Apple"
println(str)

The preceding statement outputs the following:

'A' for Apple

If you want to represent the backslash () within a string, prefix the backslash with another backslash ():

var path = "C:\WINDOWS\system32"
println(path)

The preceding statement outputs the following:

C:WINDOWSsystem32

The special character represents a tab character:

var headers = "Column 1 	 Column 2 	 Column3"
println(headers)

The preceding statement outputs the following:

Column 1     Column 2     Column3

The special character represents a newline character:

var column1 = "Row 1
Row 2
Row 3"
println(column1)

The preceding statement outputs the following:

Row 1
Row 2
Row 3

Unicode

In Swift, a Character represents a single extended grapheme cluster. An extended grapheme cluster is a sequence of one or more Unicode scalars that when combined produces a single human-readable character. Consider the following example:

let hand:Character = "u{270B}"
let star = "u{2b50}"
let bouquet = "u{1F490}"

In the preceding code snippet, the three variables are of type Character, with the first one explicitly declared. Their values are assigned using single Unicode scalars, and when output they appear as shown in Figure 3.3.

images

Figure 3.3

Here is another example:

let aGrave = "u{E0}" //---à---

In the preceding statement, aGrave represents the Latin small letter “a” with a grave: à. The same statement can also be rewritten using a pair of scalars—the letter a followed by the COMBINING GRAVE ACCENT scalar:

let aGrave = "u{61}u{300}"

In either case, the aGrave variable contains one single character. To make this point clearer, consider the following statement:

var voila = "voila"

In the preceding statement, voila contains five characters. If you append the COMBINING GRAVE ACCENT scalar to it as follows, the voila variable would still contain five characters:

voila = "voila" + "u{300}" //--- voilà---

This is because the a has been changed to à.

COMMON STRING FUNCTIONS

When dealing with strings, you often need to perform the following operations:

  • Test for string equality.
  • Test if a string starts or ends with a particular string.
  • Test if a string contains a particular string.
  • Check the length of a string.

The following sections touch on these common string operations.

Equality

In Swift, string and character comparisons are performed using the equal to operator (==) or the not equal to operator (!=). Two strings are deemed to be equal if they contain exactly the same Unicode scalars in the same order. Here is an obvious example:

var string1 = "I am a string!"
var string2 = "I am a string!"
println(string1 == string2) //---true---
println(string1 != string2) //---false---

The following example shows a comparison between two Character variables, each containing a Unicode character:

var s1 = "é"       //---é---
var s2 = "u{E9}"  //---é---
println(s1 == s2)  //---true---

The next example shows a comparison between two String variables, each including a Unicode character:

var s3 = "café"      //---café---
var s4 = "cafu{E9}" //---café---
println(s3 == s4)    //---true---

If you use a COMBINING ACUTE ACCENT scalar and apply it to the scalar that precedes it, the string would be different from one that does not use the COMBINING ACUTE ACCENT scalar, as the following code snippet shows:

var s5 = "voilà"              //--- voilà---
var s6 = "voila" + "u{300}"  //--- voila + `---
println(s5 == s6)             //---false---

let s7 = "u{E0}"             //---à---
let s8 = "u{61}u{300}"      //---a + `---
println(s7 == s8)             //---false---

Prefix and Suffix

If you want to check if a string starts with a particular string prefix, use the hasPrefix() method:

var url: String = "www.apple.com"
var prefix = "http://"
if !url.hasPrefix(prefix) {
    url = prefix + url
}
println(url)

In the preceding code snippet, the hasPrefix() method takes in a String argument and returns true if the string contains the specified string prefix.

Likewise, you can use the hasSuffix() method to check whether a string contains a particular string suffix:

var url2 = "https://developer.apple.com/library/prerelease/ios/" +
           "documentation/General/Reference/" +
           "SwiftStandardLibraryReference/"

var suffix = "/"
if url2.hasSuffix(suffix) {
    println("URL ends with (suffix)")
} else {
    println("URL does not end with (suffix)")
}

The hasPrefix() and hasSuffix() methods work correctly with Unicode characters as well:

var str = "voila" + "u{300}" //--- voila + `---
var suffix = "à"
if str.hasSuffix(suffix) {
    println("String ends with (suffix)")
} else {
    println("String does not end with (suffix)")
}

The preceding code snippet outputs the following:

String ends with à

Length

In Objective-C, you get the length/size of a string using the length property. However, in Swift, because Unicode characters do not take up the same unit of storage in memory, calling the length property on a string will not work (the length property is based on 16-bit code units). There are two ways to go about finding the length of a string in Swift:

  • Use the equivalent of the length property (from NSString) in Swift. The length property from NSString is now wrapped in Swift and available as the utf16Count property. This approach is useful if you are not dealing with Unicode characters in your string.
  • Use the length property in NSString directly. You can declare a string as an NSString instance and call the length property directly, or use the bridgeToObjectiveC() method to convert a String instance to an NSString instance.
  • Use the global countElements() function available in Swift to count the length/size of a string. The countElements() function counts the size of Unicode characters correctly.

Following are several examples. First, consider this statement:

let bouquet:Character = "u{1F490}"

Because bouquet is declared as a Character, you will not be able to use the countElements() function (the countElements() function only works for strings):

println(countElements(bouquet)) //---error---

The following statements each output 1 for the length of the strings:

var s1 = "é"                 //---é---
println(countElements(s1))   //---1---

var s2 = "u{E9}"            //---é---
println(countElements(s2))   //---1---

Whether you use a Unicode character directly or use a Unicode scalar within your string, the length of the string is still the same:

var s3 = "café"              //---café---
println(countElements(s3))   //---4---

var s4 = "cafu{E9}"         //---café---
println(countElements(s4))   //---4---

Even if you combine a Unicode scalar with a string, the countElements() function will still count the number of characters correctly, as the following statements show:

var s5 = "voilà"             //--- voilà---
println(countElements(s5))   //---5---

var s6 = "voila" + "u{300}" //--- voila + `---
println(countElements(s6))   //---5---

Substrings

One of the most common operations you perform with a string is that of extracting part of it, commonly known as a substring. Unfortunately, due to the support of Unicode characters in the String type, extracting strings from a String type is not so straightforward. This section provides an explanation of how to go about extracting part of a string.

First, consider the following swiftString:

let swiftString:String =
    "The quick brown fox jumps over the lazy dog."

Every String type has a number of properties of type String.Index. Index is a structure that contains a number of properties that point to the current character in the String variable, its next character, its previous character, and so on. The Index structure is defined as an extension to the String type:

extension String : Collection {
    struct Index : BidirectionalIndex, Reflectable {
        func successor() -> String.Index
        func predecessor() -> String.Index
        func getMirror() -> Mirror
    }
    var startIndex: String.Index { get }
    var endIndex: String.Index { get }
    subscript (i: String.Index) -> Character { get }
    func generate() -> IndexingGenerator<String>
}

To better understand the use of the Index structure, consider the following statement:

        println(swiftString[swiftString.startIndex])  //---T---

The preceding statement uses the startIndex property (of type String.Index) to refer to the first character of the string. You use it as the index to pass to the String’s subscript() method to extract the first character. Note that due to the way characters are stored in the String variable, you cannot directly specify a number indicating the position of the character that you want to extract, like this:

        println(swiftString[0])  //---error---

You can also use the endIndex property together with the predecessor() method to get the last character in the string:

println(swiftString[swiftString.endIndex.predecessor()]) //---.---

To get the character at a particular index, you can use the advance() method and specify the number of characters to move relative to a starting position:

//---start from the string's startIndex and advance 2 characters---
var index = advance(swiftString.startIndex, 2)
println(swiftString[index]) //---e---

In the preceding statements, index is of type String.Index. You make use of it to extract the character from the string.

You can also traverse the string backwards by starting at the end index and specifying a negative value to move:

        index = advance(swiftString.endIndex, -3)
        println(swiftString[index])  //---o---

The successor() method returns the position of the character after the current character:

        println(swiftString[index.successor()])    //---g---

The predecessor() method returns the position of the character prior to the current character:

        println(swiftString[index.predecessor()])  //---d---

You can also use the subStringFromIndex() method to obtain a substring starting from the specified index:

        println(swiftString.substringFromIndex(index))
        //---e quick brown fox jumps over the lazy dog.---

Likewise, to get the substring from the beginning up to the specified index, use the substringToIndex() method:

        println(swiftString.substringToIndex(index))  //---Th---

What if you want to find a range within the string? You can use the following code snippet:

    //---creates a Range<Int> instance; start index at 4 and end at 8---
    let r = Range(start: 4, end: 8)

    //---create a String.Index instance to point to the starting char---
    let startIndex = advance(swiftString.startIndex, r.startIndex)

    //---create a String.Index instance to point to the end char---
    let endIndex = advance(startIndex, r.endIndex - r.startIndex + 1)

    //---create a Range<String.Index> instance---
    var range = Range(start: startIndex, end: endIndex)

    //---extract the substring using the Range<String.Index> instance---
    println(swiftString.substringWithRange(range))  //---quick---

The preceding code snippet uses the substringWithRange() method to extract the characters starting from index 4 and ending at index 8. The substringWithRange() method takes in a Range<String.Index> instance, so you need to write a little code to create it.

If you want to find the position of a character within a string, you can use the find() method:

        //---finding the position of a character in a string---
        let char:Character = "i"
        if let charIndex = find(swiftString, char) {
            let charPosition = distance(swiftString.startIndex, charIndex)
            println(charPosition)  //---6---
        }

The find() method returns a String.Index instance. In order to translate that into an integer, you need to use the distance() method.

Converting Strings to Arrays

Another way to deal with a string’s individual character is to convert a String value into an array. The following statement shows str containing a string with a Unicode character:

var str = "voila" + "u{300}" //--- voila + `---

You can convert the string into an Array instance:

var arr = Array(str)

Once the string is converted to an array, you can access its individual characters through its index:

println(arr[4]) //---à---

Type Conversion

In Swift, there is no implicit conversion; you need to perform explicit conversion whenever you want to convert a variable from one type to another type. Consider the following statement:

var s1 = "400"

By type inherence, s1 is String. If you want to convert it into an Int type, you need to use the toInt() method to explicitly convert it:

var amount1:Int? = s1.toInt()

You must specify the ? character to indicate that this is an optional type; otherwise, the type conversion will fail. You can rewrite the preceding example to use type inference:

var amount1 = s1.toInt()

Consider another example:

var s2 = "1.25"

If you call the toInt() method to explicitly convert it to the Int type, you will get a nil:

var amount2 = s2.toInt() //---nil as string cannot be converted to Int---

If you call the toDouble() method to explicitly convert it to the Double type, you will get an error:

var amount2:Double = s2.toDouble() //---error---

This is because the String type does not have the toDouble() method. To resolve this, you can cast it to NSString and use the doubleValue property:

var amount2: Double = (s2 as NSString).doubleValue //---1.25---

What about converting from numeric values to String types? Consider the following code snippet:

var num1 = 200     //---num1 is Int---
var num2 = 1.25    //---num2 is Double---

To convert the num1 (which is of type Int), you can use the String initializer:

var s3 = String(num1)

Alternatively, you can use the string interpolation method:

var s3 = "(num1)"

To convert the num2 (which is of type Double), you cannot use the String initializer, as it does not accept an argument of type Double:

var s4 = String(num2)   //---error---

Instead, you have to use the string interpolation method:

var s4 = "(num2)"

INTEROPERABILITY WITH NSSTRING

If you are familiar with the NSString class in Objective-C, you will be happy to know that the String type in Swift is bridged seamlessly (almost) with the NSString class in the Foundation framework in Objective-C. This means that you can continue to use the methods and properties related to NSString in Swift’s String type. However, there are some caveats that you need to be aware of.

Consider the following statement:

var str1 = "This is a Swift string"

Based on type inference, str1 would be of String type. However, you can continue to use the methods and properties already available in NSString, such as the following properties:

println(str1.uppercaseString)
println(str1.lowercaseString)
println(str1.capitalizedString)

In the preceding statements, uppercaseString, lowercaseString, and capitalizedString are all properties belonging to the NSString class, but you can use them in a String instance.

Swift will also automatically convert a result from NSArray of NSStrings to the Array class in Swift, as the following example demonstrates:

var fruitsStr = "apple,orange,pineapple,durian"
var fruits = fruitsStr.componentsSeparatedByString(",")
for fruit in fruits {
    println(fruit)
}

The preceding code snippet extracts from the string an array of items separated by the comma (,). The result of type NSArray is automatically converted to the Array type in Swift. The preceding example will output the following:

apple
orange
pineapple
durian

Casting String to NSString

There are some methods from the NSString class that you need to take note of in Swift. For example, the containsString() method is available in NSString, but if you call it directly in an instance of the String type, you will get an error:

var str1 = "This is a Swift string"
println(str1.containsString("Swift"))
//---error: 'String' does not have a member named 'containsString'---

For such cases, you first need to explicitly convert the String instance to an NSString instance using the as keyword:

var str1 = "This is a Swift string"
println((str1 as NSString).containsString("Swift")) //---true---

Once you have converted the String instance to an NSString instance, you can call the containsString() method.

As described earlier in the chapter, due to the way characters are stored in a String instance, you have to use the countElements() method in Swift to get the length of a string. However, you can also use the length property available in the NSString class by type casting it to NSString:

var str1 = "This is a Swift string"
println((str1 as NSString).length)  //---22---

Another thing to be aware of is that some methods require arguments to be of a particular Swift type, even if the method is available in NSString. For example, the stringByReplacingCharactersInRange() method takes in two arguments: an instance of type Range<String.Index> and a String instance. If you call this method and pass in an NSRange instance, an error will occur:

//---an instance of NSRange---
var nsRange = NSMakeRange(5, 2)

str1.stringByReplacingCharactersInRange(nsRange, withString: "was")
//---error: 'NSRange' is not convertible to 'Range<String.Index>'---

Instead, you need to create an instance of type Range<String.Index> (a Swift type) and use it in the stringByReplacingCharactersInRange() method:

//---an instance of Range<String.Index>---
var swiftRange =
    advance(str1.startIndex, 5) ..< advance(str1.startIndex, 7)

str1 = str1.stringByReplacingCharactersInRange(
    swiftRange, withString: "was")
println(str1)    //---This was a Swift string---

Using NSString Directly

An alternative way to deal with strings is to declare a variable explicitly as an NSString type:

var str2:NSString = "This is a NSString in Objective-C. "

In the preceding statement, str2 will now be an NSString instance. The statement can also be rewritten as follows:

var str2 = "This is a NSString in Objective-C. " as NSString

You can call all the NSString methods directly via str2:

println(str2.length)                       //---35---
println(str2.containsString("NSString"))   //---true---
println(str2.hasPrefix("This"))            //---true---
println(str2.hasSuffix(". "))              //---true---
println(str2.uppercaseString)    //---THIS IS A NSSTRING IN OBJECTIVE-C.---
println(str2.lowercaseString)    //---this is a nsstring in objective-c.---
println(str2.capitalizedString)  //---This Is A Nsstring In Objective-C---

println(str2.stringByAppendingString("Yeah!"))
//---This is a NSString in Objective-C. Yeah!---

println(str2.stringByAppendingFormat("This is a number: %d", 123))
//---This is a NSString in Objective-C. This is a number: 123---

You can also create an NSRange instance and use it directly in the stringByReplacingCharactersInRange() method:

var range = str2.rangeOfString("Objective-C")
if range.location != NSNotFound {
    println("Index is (range.location) length is (range.length)")
    //---Index is 22 length is 11---
    str2 = str2.stringByReplacingCharactersInRange(
    range, withString: "Swift")
    println(str2)  //---This is a NSString in Swift.---
}

Here is another example of using the rangeOfString() method from NSString to find the index of the occurrence of a string within a string:

var path:NSString = "/Users/wei-menglee/Desktop"

//---find the index of the last /---
range = path.rangeOfString("/",
    options:NSStringCompareOptions.BackwardsSearch)

if range.location != NSNotFound {
    println("Index is (range.location)")  //---18---
}

String or NSString?

Now that you are aware of the two possible ways to deal with strings in Swift, which one should you use?

As a rule of thumb, use the String type in Swift whenever possible and feasible. The Swift language is optimized to use the String type and in most cases, you can pass a String type into methods that expect an NSString type.

If you are dealing with special characters such as emoji or Chinese characters that take up two or three bytes, it is always better to use the native String type in Swift. For example, consider the following statement:

let bouquet = "u{1F490}"

In the preceding statement, bouquet contains a single emoji (a bouquet graphic). It occupies two bytes of storage. If you want to count the number of characters contained within the string, the countElements() method counts it correctly:

println(countElements("(bouquet)"))   //---1---

However, if you use the NSString’s length property, it returns the storage required instead of the number of characters contained within the string:

println((bouquet as NSString).length)  //---2---

Likewise, in one of the examples earlier in the chapter, if you append the COMBINING GRAVE ACCENT scalar to a string, the countElements() method will count the characters correctly, whereas the length property does not:

var s6 = "voila" + "u{300}"       //--- voila + `---
println(countElements(s6))         //---5---
println((s6 as NSString).length)   //---6---

Using the native String type also enables you to use the various string features (such as string concatenation using the + operator, the For-In loop for character iteration, etc.) introduced in Swift. Once you explicitly declare a variable as NSString, you lose all these features. For example, for NSString types you cannot concatenate strings using the + operator:

var s:NSString =  "Swift"
s += " Programming"    //---not allowed---

Nor can you cannot iterate through an NSString using the For-In loop:

var s:NSString =  "Swift"
for c in s {
    println(c)
}

If you want to have the best of both worlds, always create a native String instance and then typecast to NSString to call the NSString’s methods whenever necessary.

SUMMARY

In this chapter, you have learned about the String type in Swift and how it interoperates with the NSString class, with which most Objective-C developers are familiar. While you may be tempted to use the various string libraries that you are already familiar with in Objective-C, it is always a good idea to familiarize yourself with the various String methods in Swift so that your code is future-proof. Also, as the Swift language evolves, you should be seeing more enhancements to the language as Apple ramps up adoption of the language.

EXERCISES

  1. Given the following statement, write the solution to find out the position of the “q” character in the string:

            var str1 = "The quick brown fox jumps over the lazy dog"
  2. The following code snippet causes the compiler to flag an error. Fix it.

            var amount = "1200"
            var rate = "1.27"
            var result = amount * rate
  3. Given the following variables, write the statement to output the following:

            var lat = 40.765819
            var lng = -73.975866
            println("<fill in the blanks>")
            // Lat/Lng is (40.765819, -73.975866)

arrow2 WHAT YOU LEARNED IN THIS CHAPTER

TOPIC KEY CONCEPTS
Representing a string You can represent a string using the String or NSString type.
Mutability of strings Strings created using the let keyword are immutable. Strings created using the var keyword are mutable.
String as a value type Strings are represented as a value type in Swift; when they are assigned to another variable/constant, a copy is made.
Representing characters You can represent a character using the Character type.
Concatenating strings Strings can be conveniently concatenated using the + or += operator.
Unicode The String type in Swift is capable of representing Unicode characters.
String equality To test the equality of two strings, use the == or != operator.
Length of a string To find out the length of a string, you can either cast it to a NSString and then use the length property, or you can use the countElements() function in Swift.
Converting a string to an array Another way to deal with a string is to convert it into an array.
String to number conversion Use the toInt() method to convert a string to an integer. To convert a string into a double, cast the string to NSString and then use the doubleValue property.
Casting from String to NSString Use the as keyword to cast a String to NSString.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.10