Type Aliases

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

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.

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

The C 'char' type.

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

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

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

The C 'double' type.

The C 'float' type.

The C 'int' type.

The C 'long' type.

The C 'long long' type.

The C 'short' type.

The C 'signed char' type.

The C 'unsigned char' type.

The C 'unsigned int' type.

The C 'unsigned long' type.

The C 'unsigned long long' type.

The C 'unsigned short' type.

The C++ 'wchar_t' type.

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

A 32-bit floating point type.

A 64-bit floating point type.

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

The largest native signed integer type.

The default type for an otherwise-unconstrained integer literal.

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

The default type for an otherwise-unconstrained string literal.

The largest native unsigned integer type.

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

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

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"