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

Get Swift Quick Syntax Reference 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.