Generic Instance Method

withCString(_:)

Calls the given closure with a pointer to the contents of the string, represented as a null-terminated sequence of UTF-8 code units.

Declaration

func withCString<Result>(_ body: (UnsafePointer<Int8>) throws -> Result) rethrows -> Result

Parameters

body

A closure with a pointer parameter that points to a null-terminated sequence of UTF-8 code units. If body has a return value, that value is also used as the return value for the withCString(_:) method. The pointer argument is valid only for the duration of the method’s execution.

Return Value

The return value, if any, of the body closure parameter.

Discussion

The pointer passed as an argument to body is valid only during the execution of withCString(_:). Do not store or return the pointer for later use.

Relationships

From Protocol

See Also

Getting C Strings

var utf8CString: ContiguousArray<CChar>

A contiguously stored null-terminated UTF-8 representation of the string.

func withCString<Result, TargetEncoding>(encodedAs: TargetEncoding.Type, (UnsafePointer<TargetEncoding.CodeUnit>) -> Result) -> Result

Calls the given closure with a pointer to the contents of the string, represented as a null-terminated sequence of code units.