Type Aliases

typealias AnyClass = AnyObject.Type

The protocol to which all class types implicitly conform.

You can use the AnyClass protocol as the concrete type for an instance of any class. When you do, all known @objc class methods and properties are available as implicitly unwrapped optional methods and properties, respectively. For example:

class IntegerRef {
    @objc class func getDefaultValue() -> Int {
        return 42
    }
}

func getDefaultValue(_ c: AnyClass) -> Int? {
    return c.getDefaultValue?()
}

The getDefaultValue(_:) function uses optional chaining to safely call the implicitly unwrapped class method on c. Calling the function with different class types shows how the getDefaultValue() class method is only conditionally available.

print(getDefaultValue(IntegerRef.self))
// Prints "Optional(42)"

print(getDefaultValue(NSString.self))
// Prints "nil"

See Also: AnyObject

typealias ArrayLiteralConvertible = ExpressibleByArrayLiteral
typealias BidirectionalIndexable = _BidirectionalIndexable

A type that provides subscript access to its elements, with bidirectional index traversal.

In most cases, it's best to ignore this protocol and use the BidirectionalCollection protocol instead, because it has a more complete interface.

Deprecated: it will be removed in Swift 4.0. Please use 'BidirectionalCollection' instead.

typealias BooleanLiteralConvertible = ExpressibleByBooleanLiteral
typealias BooleanLiteralType = Bool

The default type for an otherwise-unconstrained Boolean literal.

When you create a constant or variable using one of the Boolean literals true or false, the resulting type is determined by the BooleanLiteralType alias. For example:

let isBool = true
print("isBool is a '\(type(of: isBool))'")
// Prints "isBool is a 'Bool'"

The type aliased by BooleanLiteralType must conform to the ExpressibleByBooleanLiteral protocol.

typealias CBool = Bool

The C '_Bool' and C++ 'bool' type.

typealias CChar = Int8

The C 'char' type.

This will be the same as either CSignedChar (in the common case) or CUnsignedChar, depending on the platform.

typealias CChar16 = UInt16

The C++11 'char16_t' type, which has UTF-16 encoding.

typealias CChar32 = UnicodeScalar

The C++11 'char32_t' type, which has UTF-32 encoding.

typealias CDouble = Double

The C 'double' type.

typealias CFloat = Float

The C 'float' type.

typealias CInt = Int32

The C 'int' type.

typealias CLong = Int
typealias CLongLong = Int64
typealias CShort = Int16

The C 'short' type.

typealias CSignedChar = Int8

The C 'signed char' type.

typealias CUnsignedChar = UInt8

The C 'unsigned char' type.

typealias CUnsignedInt = UInt32

The C 'unsigned int' type.

typealias CUnsignedLong = UInt

The C 'unsigned long' type.

typealias CUnsignedLongLong = UInt64

The C 'unsigned long long' type.

typealias CUnsignedShort = UInt16

The C 'unsigned short' type.

typealias CWideChar = UnicodeScalar

The C++ 'wchar_t' type.

typealias DictionaryIndex<Key,
typealias DictionaryLiteralConvertible = ExpressibleByDictionaryLiteral
typealias ExpressibleByStringInterpolation

A type that can be initialized by string interpolation with a string literal that includes expressions.

Use string interpolation to include one or more expressions in a string literal, wrapped in a set of parentheses and prefixed by a backslash. For example:

let price = 2
let number = 3
let message = "One cookie: $\(price), \(number) cookies: $\(price * number)."
print(message)
// Prints "One cookie: $2, 3 cookies: $6."

Conforming to the ExpressibleByStringInterpolation Protocol

To use string interpolation to initialize instances of your custom type, implement the required initializers for ExpressibleByStringInterpolation conformance. String interpolation is a multiple-step initialization process. When you use string interpolation, the following steps occur:

  1. The string literal is broken into pieces. Each segment of the string literal before, between, and after any included expressions, along with the individual expressions themselves, are passed to the init(stringInterpolationSegment:) initializer.
  2. The results of those calls are passed to the init(stringInterpolation:) initializer in the order in which they appear in the string literal.

In other words, initializing the message constant in the example above using string interpolation is equivalent to the following code:

let message = String(stringInterpolation:
      String(stringInterpolationSegment: "One cookie: $"),
      String(stringInterpolationSegment: price),
      String(stringInterpolationSegment: ", "),
      String(stringInterpolationSegment: number),
      String(stringInterpolationSegment: " cookies: $"),
      String(stringInterpolationSegment: price * number),
      String(stringInterpolationSegment: "."))

Deprecated: it will be replaced or redesigned in Swift 4.0. Instead of conforming to 'ExpressibleByStringInterpolation', consider adding an 'init(_:String)'.

typealias ExtendedGraphemeClusterLiteralConvertible = ExpressibleByExtendedGraphemeClusterLiteral
typealias ExtendedGraphemeClusterType = String

The default type for an otherwise-unconstrained Unicode extended grapheme cluster literal.

typealias Float32 = Float

A 32-bit floating point type.

typealias Float64 = Double

A 64-bit floating point type.

typealias FloatLiteralConvertible = ExpressibleByFloatLiteral
typealias FloatLiteralType = Double

The default type for an otherwise-unconstrained floating point literal.

typealias Indexable = _Indexable

A type that provides subscript access to its elements, with forward index traversal.

In most cases, it's best to ignore this protocol and use the Collection protocol instead, because it has a more complete interface.

Deprecated: it will be removed in Swift 4.0. Please use 'Collection' instead.

typealias IndexableBase = _IndexableBase

A type that provides subscript access to its elements, with forward index traversal.

In most cases, it's best to ignore this protocol and use the Collection protocol instead, because it has a more complete interface.

Deprecated: it will be removed in Swift 4.0. Please use 'Collection' instead.

typealias IntMax = Int64

The largest native signed integer type.

typealias IntegerLiteralConvertible = ExpressibleByIntegerLiteral
typealias IntegerLiteralType = Int

The default type for an otherwise-unconstrained integer literal.

typealias MutableIndexable = _MutableIndexable

A type that provides subscript access to its elements.

In most cases, it's best to ignore this protocol and use the MutableCollection protocol instead, because it has a more complete interface.

Deprecated: it will be removed in Swift 4.0. Please use 'MutableCollection' instead.

typealias NilLiteralConvertible = ExpressibleByNilLiteral
typealias RandomAccessIndexable = _RandomAccessIndexable

A collection that supports efficient random-access index traversal.

In most cases, it's best to ignore this protocol and use the RandomAccessCollection protocol instead, because it has a more complete interface.

Deprecated: it will be removed in Swift 4.0. Please use 'RandomAccessCollection' instead.

typealias RangeReplaceableIndexable = _RangeReplaceableIndexable

A type that supports replacement of an arbitrary subrange of elements with the elements of another collection.

In most cases, it's best to ignore this protocol and use the RangeReplaceableCollection protocol instead, because it has a more complete interface.

Deprecated: it will be removed in Swift 4.0. Please use 'RandomAccessCollection' instead.

typealias SetIndex<Element> = Set<Element>.Index
typealias StringInterpolationConvertible = ExpressibleByStringInterpolation

Deprecated: it will be replaced or redesigned in Swift 4.0. Instead of conforming to 'StringInterpolationConvertible', consider adding an 'init(_:String)'.

typealias StringLiteralConvertible = ExpressibleByStringLiteral
typealias StringLiteralType = String

The default type for an otherwise-unconstrained string literal.

typealias UIntMax = UInt64

The largest native unsigned integer type.

typealias UnfoldFirstSequence<T> = UnfoldSequence<T, (T?, Bool)>

The return type of sequence(first:next:).

typealias UnicodeScalarLiteralConvertible = ExpressibleByUnicodeScalarLiteral
typealias UnicodeScalarType = String

The default type for an otherwise-unconstrained unicode scalar literal.

typealias Void = ()

The return type of functions that don't explicitly specify a return type; an empty tuple (i.e., ()).

When declaring a function or method, you don't need to specify a return type if no value will be returned. However, the type of a function, method, or closure always includes a return type, which is Void if otherwise unspecified.

Use Void or an empty tuple as the return type when declaring a closure, function, or method that doesn't return a value.

// No return type declared:
func logMessage(_ s: String) {
    print("Message: \(s)")
}

let logger: (String) -> Void = logMessage
logger("This is a void function")
// Prints "Message: This is a void function"