O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

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

Chapter    10

Type Aliases

You can add an alternative name for existing types using type aliases. This is something you might do if you want to make your API more expressive and clearer to other programmers. For instance, let’s say you had a type of string that you wanted to treat as a note in your code. You could use a type alias to define Note as an alternative to String.

Use the typealias keyword to define a type alias, as shown Listing 10-1.

Listing 10-1. Type Alias

typealias Note = String

Now that you’ve defined the type alias, you can use the type Note in place of String when you are declaring new variables, constants, or optionals (see Listing 10-2).

Listing 10-2. Using Type Aliases

typealias Note = Stringvar n1:Note = "Today is the first ...

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