• Skip to main content
  • Skip to footer

AndyBargh.com

Exploring the World of Swift and iOS Development

  • About
    • About Me
    • Contact
  • Swift Developments
    • Back Issues
  • Blog
    • Latest Articles
You are here: Home / Swift / Swift Type Aliases

Swift Type Aliases

Using Custom Types as Enumeration Case Raw Values in Swift

Just a quick post today looking at type aliases in Swift.

Type aliases, as the name suggests, are a way of providing an alternative name or alias for an existing Swift type.

As such, type aliases provide a convenient way for referring to existing types using a name that is more appropriate to the particular context we’re working with and in doing so can make our API’s more expressive, meaningful and clearer for other programmers reading our code.

In Swift, type alias declarations have the following general form:

typealias NewAlias = ExistingTypeName

To define a type alias we use the typealias keyword followed by the name of the new type alias we wish to define.

Telling Swift we’re declaring a type alias is only half the story though. We also need to tell Swift what type the new name will be an alias for.

To do this, we use the assignment operator followed by the name of the type we wish to create an alias for.

The thing to remember is that when we declare a type alias in Swift we are not actually declaring a new Swift type. Instead, we are just providing another name or label for the existing type.

With that said, type aliases can and usually are used in place of the types for which they are aliases and the implications are therefore that we should follow the standard Swift type naming rules when naming them (namely that their names follow an UpperCamelCase naming convention).

So that’s the theory, let’s have a look at the practice.

Say we had a function that returned a UInt value to represent a players score in a game. The function might look something like this:

fun calculateScore() -> UInt {
    // Calculate the score…
    return 10
}

To make the API clearer though, we could define a new type alias called Score that we could use as an alias for the UInt type:

typealias Score = UInt

Now that we’ve declared the type alias we can now use it anywhere we were using UInt previously, namely in place of the UInt in the return type:

func calculateScore() -> Score { 
    // Calculate the score… 
    return 10
}

Swift’s type inference mechanism also works and correctly infers using our new type alias:

let playerOneScore = calculateScore()
// palyerOneScore inferred to be of type Score.

But because Score is just an alias for the UInt type, the following is also legal:

let playerScore : UInt = calculateScore()

And since Score is just another label for the UInt type, we also still have access to all the existing methods on the UInt type:

print(Score.max)
// prints 18446744073709551615

Footer CTA

  • E-mail
  • GitHub
  • Pinterest
  • Twitter
  • YouTube
  • About
  • Terms of Service
  • Privacy Policy
  • Contact

Copyright © 2023 · Academy Pro on Genesis Framework · WordPress · Log in