A method identified as deprecated has been superseded and may become unsupported in the future.
Returns a string created by reading data from the file named by a given path. (Deprecated in Mac OS X v10.4. Use stringWithContentsOfFile:encoding:error:
or stringWithContentsOfFile:usedEncoding:error: instead.)
+ (id)stringWithContentsOfFile:(NSString *)path
If the contents begin with a Unicode byte-order mark (U+FEFF
or U+FFFE
), interprets the contents as Unicode characters. If the contents begin with a UTF-8 byte-order mark (EFBBBF
), interprets the contents as UTF-8. Otherwise, interprets the contents as data in the default C string encoding. Since the default C string encoding will vary with the user’s configuration, do not depend on this method unless you are using Unicode or UTF-8 or you can verify the default C string encoding. Returns nil
if the file can’t be opened.
NSString.h
Returns a string created by reading data from the file named by a given URL. (Deprecated in Mac OS X v10.4. Use stringWithContentsOfURL:encoding:error:
or stringWithContentsOfURL:usedEncoding:error:
instead.)
+ (id)stringWithContentsOfURL:(NSURL *)aURL
If the contents begin with a byte-order mark (U+FEFF or U+FFFE), interprets the contents as Unicode characters. If the contents begin with a UTF-8 byte-order mark (EFBBBF), interprets the contents as UTF-8. Otherwise interprets the contents as data in the default C string encoding. Since the default C string encoding will vary with the user’s configuration, do not depend on this method unless you are using Unicode or UTF-8 or you can verify the default C string encoding. Returns nil
if the location can’t be opened.
NSString.h
Creates a new string using a given C-string. (Deprecated in Mac OS X v10.4. Use stringWithCString:encoding:
instead.)
+ (id)stringWithCString:(const char *)cString
cString should contain data in the default C string encoding. If the argument passed to stringWithCString:
is not a zero-terminated C-string, the results are undefined.
NSString.h
Returns a string containing the characters in a given C-string. (Deprecated in Mac OS X v10.4. Use stringWithCString:encoding:
instead.)
+ (id)stringWithCString:(const char *)cString length:(NSUInteger)length
cString must not be NULL. cString should contain characters in the default C-string encoding. This method converts length * sizeof(char)
bytes from cString and doesn’t stop short at a NULL
character.
NSString.h
Returns a representation of the receiver as a C string in the default C-string encoding. (Deprecated in Mac OS X v10.4. Use cStringUsingEncoding:
or UTF8String
instead.)
- (const char *)cString
The returned C string will be automatically freed just as a returned object would be released; your code should copy the C string or use getCString:
if it needs to store the C string outside of the autorelease context in which the C string is created.
Raises an NSCharacterConversionException
if the receiver can’t be represented in the default C-string encoding without loss of information. Use canBeConvertedToEncoding:
if necessary to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use lossyCString
or dataUsingEncoding:allowLossyConversion:
to get a C-string representation with some loss of information.
NSString.h
Returns the length in char-sized units of the receiver’s C-string representation in the default C-string encoding. (Deprecated in Mac OS X v10.4. Use lengthOfBytesUsingEncoding: or maximumLengthOfBytesUsingEncoding: instead.)
- (NSUInteger)cStringLength
Raises if the receiver can’t be represented in the default C-string encoding without loss of information. You can also use canBeConvertedToEncoding:
to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use lossyCString
to get a C-string representation with some loss of information, then check its length explicitly using the ANSI function strlen()
.
NSString.h
Invokes getCString:maxLength:range:remainingRange:
with NSMaximumStringLength
as the maximum length, the receiver’s entire extent as the range, and NULL
for the remaining range. (Deprecated in Mac OS X v10.4. Use cStringUsingEncoding: or dataUsingEncoding:allowLossyConversion:
instead.)
- (void)getCString:(char *)buffer
buffer must be large enough to contain the resulting C-string plus a terminating NULL character (which this method adds—[string cStringLength]
).
Raises an NSCharacterConversionException
if the receiver can’t be represented in the default C-string encoding without loss of information. Use canBeConvertedToEncoding:
if necessary to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use lossyCString
or dataUsingEncoding:allowLossyConversion:
to get a C-string representation with some loss of information.
NSString.h
Invokes getCString:maxLength:range:remainingRange:
with maxLength as the maximum length in char-sized units, the receiver’s entire extent as the range, and NULL
for the remaining range. (Deprecated in Mac OS X v10.4. Use getCString:maxLength:encoding: instead.)
- (void)getCString:(char *)buffer maxLength:(NSUInteger)maxLength
buffer must be large enough to contain maxLength chars plus a terminating zero char (which this method adds).
Raises an NSCharacterConversionException
if the receiver can’t be represented in the default C-string encoding without loss of information. Use canBeConvertedToEncoding:
if necessary to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use lossyCString
or dataUsingEncoding:allowLossyConversion:
to get a C-string representation with some loss of information.
NSString.h
Converts the receiver’s content to the default C-string encoding and stores them in a given buffer. (Deprecated in Mac OS X v10.4. Use getCString:maxLength:encoding: instead.)
- (void)getCString:(char *)buffer maxLength:(NSUInteger)maxLength range:(NSRange)aRange remainingRange:(NSRangePointer)leftoverRange
buffer must be large enough to contain maxLength bytes plus a terminating zero character (which this method adds). Copies and converts as many characters as possible from aRange and stores the range of those not converted in the range given by leftoverRange (if it’s non-nil
). Raises an NSRangeException
if any part of aRange lies beyond the end of the string.
Raises an NSCharacterConversionException
if the receiver can’t be represented in the default C-string encoding without loss of information. Use canBeConvertedToEncoding:
if necessary to check whether a string can be losslessly converted to the default C-string encoding. If it can’t, use lossyCString
or dataUsingEncoding:allowLossyConversion:
to get a C-string representation with some loss of information.
NSString.h
Initializes the receiver, a newly allocated NSString
object, by reading data from the file named by path. (Deprecated in Mac OS X v10.4. Use initWithContentsOfFile:encoding:error:
or initWithContentsOfFile:usedEncoding:error:
instead.)
- (id)initWithContentsOfFile:(NSString *)path
Initializes the receiver, a newly allocated NSString
object, by reading data from the file named by path. If the contents begin with a byte-order mark (U+FEFF
or U+FFFE
), interprets the contents as Unicode characters; otherwise interprets the contents as data in the default C string encoding. Returns an initialized object, which might be different from the original receiver, or nil
if the file can’t be opened.
NSString.h
Initializes the receiver, a newly allocated NSString
object, by reading data from the location named by a given URL. (Deprecated in Mac OS X v10.4. Use initWithContentsOfURL:encoding:error:
or initWithContentsOfURL:usedEncoding:error:
instead.)
- (id)initWithContentsOfURL:(NSURL *)aURL
Initializes the receiver, a newly allocated NSString
object, by reading data from the location named by aURL. If the contents begin with a byte-order mark (U+FEFF
or U+FFFE
), interprets the contents as Unicode characters; otherwise interprets the contents as data in the default C string encoding. Returns an initialized object, which might be different from the original receiver, or nil
if the location can’t be opened.
NSString.h
Initializes the receiver, a newly allocated NSString
object, by converting the data in a given C-string from the default C-string encoding into the Unicode character encoding. (Deprecated in Mac OS X v10.4. Use initWithCString:encoding:
instead.)
- (id)initWithCString:(const char *)cString
cString must be a zero-terminated C string in the default C string encoding, and may not be NULL
. Returns an initialized object, which might be different from the original receiver.
To create an immutable string from an immutable C string buffer, do not attempt to use this method. Instead, use initWithCStringNoCopy:length:freeWhenDone:
.
NSString.h
Initializes the receiver, a newly allocated NSString
object, by converting the data in a given C-string from the default C-string encoding into the Unicode character encoding. (Deprecated in Mac OS X v10.4. Use initWithCString:encoding: instead.)
- (id)initWithCString:(const char *)cString length:(NSUInteger)length
This method converts length * sizeof(char)
bytes from cString and doesn’t stop short at a zero character. cString must contain bytes in the default C-string encoding and may not be NULL
. Returns an initialized object, which might be different from the original receiver.
NSString.h
Initializes the receiver, a newly allocated NSString
object, by converting the data in a given C-string from the default C-string encoding into the Unicode character encoding. (Deprecated in Mac OS X v10.4. Use initWithBytesNoCopy:length:encoding:freeWhenDone:
instead.)
- (id)initWithCStringNoCopy:(char *)cString length:(NSUInteger)length freeWhenDone:(BOOL)flag
This method converts length * sizeof(char)
bytes from cString and doesn’t stop short at a zero character. cString must contain data in the default C-string encoding and may not be NULL
. The receiver becomes the owner of cString; if flag is YES
it will free the memory when it no longer needs it, but if flag is NO
it won’t. Returns an initialized object, which might be different from the original receiver.
You can use this method to create an immutable string from an immutable (const char *
) C-string buffer. If you receive a warning message, you can disregard it; its purpose is simply to warn you that the C string passed as the method’s first argument may be modified. If you make certain the freeWhenDone
argument to initWithStringNoCopy
is NO
, the C string passed as the method’s first argument cannot be modified, so you can safely use initWithStringNoCopy
to create an immutable string from an immutable (const char *
) C-string buffer.
NSString.h
Returns a representation of the receiver as a C string in the default C-string encoding, possibly losing information in converting to that encoding. (Deprecated in Mac OS X v10.4. Use cStringUsingEncoding: or dataUsingEncoding:allowLossyConversion:
instead.)
- (const char *)lossyCString
This method does not raise an exception if the conversion is lossy. The returned C string will be automatically freed just as a returned object would be released; your code should copy the C string or use getCString:
if it needs to store the C string outside of the autorelease context in which the C string is created.
NSString.h
Writes the contents of the receiver to the file specified by a given path. (Deprecated in Mac OS X v10.4. Use writeToFile:atomically:encoding:error:
instead.)
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
YES
if the file is written successfully, otherwise NO
.
Writes the contents of the receiver to the file specified by path (overwriting any existing file at path). path is written in the default C-string encoding if possible (that is, if no information would be lost), in the Unicode encoding otherwise.
If flag is YES
, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. If flag is NO
, the receiver is written directly to path. The YES
option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.
If path contains a tilde (~
) character, you must expand it with stringByExpandingTildeInPath
before invoking this method.
NSString.h
Writes the contents of the receiver to the location specified by a given URL. (Deprecated in Mac OS X v10.4. Use writeToURL:atomically:encoding:error:
instead.)
- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)atomically
YES
if the location is written successfully, otherwise NO
.
If atomically is YES
, the receiver is written to an auxiliary location, and then the auxiliary location is renamed to aURL. If atomically is NO
, the receiver is written directly to aURL. The YES
option guarantees that aURL, if it exists at all, won’t be corrupted even if the system should crash during writing.
The atomically parameter is ignored if aURL is not of a type that can be accessed atomically.
NSString.h
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-02-04)