Strings

In Swift, the syntax around using Strings is very much simpler than in Objective C, and the basics are similar to many modern languages.

We can create a string literal by using double quotes, as follows:

let salutation = "Hi Sally" 

Single quotes won't do it:

let salutation = 'Hi Bob' // error 

We can declare the empty string as an empty literal:

var emptyString1 = "" 

We can also declare it by using the String initializer:

var emptyString2 = String() 

The empty String object is not nil. To declare a String with no initialization, producing a nil String object, we do the following:

var nilString: String 

To check for an empty (but non-nil) string, we can use  the .isEmpty method of String:

emptyString1.isEmpty //returns true 

A String comparison is simpler than comparing NSString objects:

emptyString1 == emptyString2 
emptyString1 != emptyString2
"a" < "b"

As you can see, the < and > operators behave as one would wish them to, with the preceding code evaluating to true.

String objects are like any other objects in Swift; they can be declared to be mutable or immutable with var and let, respectively.

String concatenation is similarly intuitive:

let greeting = "Hello " + "World" 

(There we are, we have got Hello World out of the way.)

String has methods for changing the case of a String object:

let upper = greeting.uppercased() 
let lower = greeting.lowercased()

String representations of other objects in Swift are mercifully simple, by including the object in  \(...) within the quotes:

let count = 11 
let average = 1.2
let message = "\( count) tasks, average grade \( average)"

This prints to the console:

11 tasks, average grade 1.2 

Unlike NSString objects in Objective C, String objects are passed by value, not reference (see the Passed by value section of this chapter).

Much was made of the fact that Swift will accept any Unicode characters, including emojis, making the following a perfectly legal String:

let s2 = "Anyone fancy ?" 

But this also means that a lot of Unicode goodness is available to us with no extra work, and we'll be looking at this in more depth later on.

A String is not an array; you cannot subscript it with myString[0] or similar. To access individual Characters, use the  .characters property of String, which is a CharacterView Struct, similar to an Array, more of which in Chapter 5, Advanced Swift:

let name = "Babbage" 
let charlesChars = name.characters

Declaring a Character literal requires explicit typing (since it would otherwise produce a String of length 1):

let dollarCharacter: Character = "$" 

However, once the type is set, reassignment looks the same as String reassignment:

var currencyCharacter: Character = "$" 
currencyCharacter = "£"
It might be a good idea to name the variable appropriately, to avoid confusion.

Use the append method of String to append a Character to a String:

var region = "NZ" 
region.append(dollarCharacter)

The region variable is now "NZ$".

We will take a closer look at the String and Character types in Chapter 5, Advanced Swift (and there is much to look at), but it's worth mentioning here, for readers familiar with Objective C, that String objects can be cast to NSString objects in order to access the full functionality of the latter.