Initializer

init(contentsOf:usedEncoding:)

Returns an NSString object initialized by reading data from a given URL and returns by reference the encoding used to interpret the data.

Declaration

convenience init(contentsOf url: URL, usedEncoding enc: UnsafeMutablePointer<UInt>?) throws

Parameters

url

The URL from which to read data.

enc

Upon return, if url is read successfully, contains the encoding used to interpret the data. For possible values, see NSStringEncoding.

error

If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.

Return Value

An NSString object initialized by reading data from url. If url can’t be opened or the encoding cannot be determined, returns nil. The returned initialized object might be different from the original receiver

Discussion

To read data with an unknown encoding, pass 0 as the enc parameter as in:

    NSURL *textFileURL = …;
    NSStringEncoding encoding = 0;
    NSError *error = nil;
    NSString *string = [[NSString alloc] initWithContentsOfURL:textFileURL usedEncoding:&encoding error:&error];

See Also

Related Symbols

stringWithContentsOfURL:usedEncoding:error:

Returns a string created by reading data from a given URL and returns by reference the encoding used to interpret the data.