Initializerswift 6.0.1Swift

    init(validatingUTF8:)

    Creates a new string by copying and validating the null-terminated UTF-8 data referenced by the given pointer.

    This declaration has been renamed to String.init(validatingCString:).

    This declaration was deprecated in Swift 6.

    init?(validatingUTF8 cString: UnsafePointer<CChar>)

    Parameters

    cString

    A pointer to a null-terminated sequence of UTF-8 code units.

    This initializer does not try to repair ill-formed UTF-8 code unit sequences. If any are found, the result of the initializer is nil.

    The following example calls this initializer with pointers to the contents of two different CChar arrays—the first with well-formed UTF-8 code unit sequences and the second with an ill-formed sequence at the end.

    let validUTF8: [CChar] = [67, 97, 102, -61, -87, 0]
    validUTF8.withUnsafeBufferPointer { ptr in
        let s = String(validatingUTF8: ptr.baseAddress!)
        print(s)
    }
    // Prints "Optional("Café")"
    
    let invalidUTF8: [CChar] = [67, 97, 102, -61, 0]
    invalidUTF8.withUnsafeBufferPointer { ptr in
        let s = String(validatingUTF8: ptr.baseAddress!)
        print(s)
    }
    // Prints "nil"

    Other members in extension

    Types

    Typealiases

    Show obsolete interfaces (2)

    Hide obsolete interfaces

    Type members

    Show obsolete interfaces (16)

    Hide obsolete interfaces

    Instance members

    Show obsolete interfaces (3)

    Hide obsolete interfaces