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 ...

Get Mastering macOS Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.