O'Reilly logo

Mastering Swift by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The string type

A string is an ordered collection of characters, such as Hello or Swift. In Swift, the string type represents a string. We have seen several examples of strings already in this book, so the following code should look familiar. This code shows how to define two strings:

var stringOne = "Hello"
var stringTwo = " World"

Since a string is an ordered collection of characters, we can iterate through each character of a string. The following code shows how to do this:

var stringOne = "Hello"
for char in stringOne {
  println(char)
}

The preceding code will display the results shown in the following screenshot:

The string type

There are two ways to add one string ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required