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

The C 'long' type.

typealias CLongLong = Int64

The C 'long long' type.

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 DictionaryLiteralConvertible = ExpressibleByDictionaryLiteral
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 IntMax = Int64

The largest native signed integer type.

typealias IntegerLiteralConvertible = ExpressibleByIntegerLiteral
typealias IntegerLiteralType = Int

The default type for an otherwise-unconstrained integer literal.

typealias NilLiteralConvertible = ExpressibleByNilLiteral
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"