C Interoperability

Use imported C types or call C variadic functions.

Topics

C and Objective-C Pointers

struct OpaquePointer

A wrapper around an opaque C pointer.

struct AutoreleasingUnsafeMutablePointer

A mutable pointer addressing an Objective-C reference that doesn’t own its target.

C Variadic Functions

func withVaList<R>([CVarArg], (CVaListPointer) -> R) -> R

Invokes the given closure with a C va_list argument derived from the given array of arguments.

struct CVaListPointer

A wrapper around a C va_list pointer.

protocol CVarArg

A type whose instances can be encoded, and appropriately passed, as elements of a C va_list.

func getVaList([CVarArg]) -> CVaListPointer

Returns a CVaListPointer that is backed by autoreleased storage, built from the given array of arguments.

Pointers to Values

Access a pointer to a variable in memory for explicit passing to C functions.

func withUnsafePointer<T, Result>(to: inout T, (UnsafePointer<T>) -> Result) -> Result

Invokes the given closure with a pointer to the given argument.

func withUnsafeMutablePointer<T, Result>(to: inout T, (UnsafeMutablePointer<T>) -> Result) -> Result

Calls the given closure with a mutable pointer to the given argument.

func withUnsafeBytes<T, Result>(of: inout T, (UnsafeRawBufferPointer) -> Result) -> Result

Invokes the given closure with a buffer pointer covering the raw bytes of the given argument.

func withUnsafeMutableBytes<T, Result>(of: inout T, (UnsafeMutableRawBufferPointer) -> Result) -> Result

Invokes the given closure with a mutable buffer pointer covering the raw bytes of the given argument.

Aliases for Imported C Types

typealias CBool

The C ‘_Bool’ and C++ ‘bool’ type.

typealias CChar

The C ‘char’ type.

typealias CChar16

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

typealias CChar32

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

typealias CDouble

The C ‘double’ type.

typealias CFloat

The C ‘float’ type.

typealias CInt

The C ‘int’ type.

typealias CLong

The C ‘long’ type.

typealias CLongLong

The C ‘long long’ type.

typealias CShort

The C ‘short’ type.

typealias CSignedChar

The C ‘signed char’ type.

typealias CUnsignedChar

The C ‘unsigned char’ type.

typealias CUnsignedInt

The C ‘unsigned int’ type.

typealias CUnsignedLong

The C ‘unsigned long’ type.

typealias CUnsignedLongLong

The C ‘unsigned long long’ type.

typealias CUnsignedShort

The C ‘unsigned short’ type.

typealias CWideChar

The C++ ‘wchar_t’ type.

See Also

Programming Tasks

Input and Output

Print values to the console, read from and write to text streams, and use command line arguments.

Debugging and Reflection

Fortify your code with runtime checks, and examine your values' runtime representation.

Key-Path Expressions

Use key-path expressions to access properties dynamically.

Manual Memory Management

Allocate and manage memory manually.

Type Casting and Existential Types

Perform casts between types or represent values of any type.

Operator Declarations

Work with prefix, postfix, and infix operators.