cString vs UTF8String (memory question)
cString vs UTF8String (memory question)
- Subject: cString vs UTF8String (memory question)
- From: Greg Hurrell <email@hidden>
- Date: Fri, 12 Dec 2003 15:54:01 +0100
Following up on a recent thread on the soon-to-be-deprecated -cString
methods in NSString, I have a question about the endorsed alternative,
-UTF8String. Now, the header for NSString
(/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/
NSString.h) says:
/* The "cString" methods are discouraged and will be deprecated in the
near future. These methods use [NSString defaultCStringEncoding] as
the encoding to convert to,
which means the results depend on the user's language and potentially
other settings. This might be appropriate in some cases, but often
these methods are misused, res
ulting in issues when running in languages other then English.
UTF8String in general is a much better choice when converting
arbitrary NSStrings into 8-bit representat
ions.
*/
In short, my question is, can I basically go through my code and
replace all calls to -cString or -lossyCString with calls to
-UTF8String instead? Are there going to be any unforeseen consequences?
(In my case the only C strings I work with tend to be filesystem paths,
for which I am already using -fileSystemRepresentation, and arguments
to command-line tools.)
I ask because I am a little confused by the documentation. The NSString
documentation for the -UTF8String method
(file:///Developer/Documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSString.html#//apple_ref/doc/uid/20000154/
CJBHCCED) only says:
A convenience method to return a null-terminated UTF8 representation
of the receiver.
Am I to assume that the representation returned will be automatically
freed as it would be in the case of the -cString method? Compare this
with the docs for that method
(file:///Developer/Documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSString.html#//apple_ref/doc/uid/20000154/
cString), which explicitly say:
Returns a representation of the receiver as a C string in the default
C-string encoding. 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.
And are calls to -UTF8String and -fileSystemRepresentation basically
interchangeable? The docs say that the latter returns a "canonical
UTF-8 encoding". Is the representation returned by -UTF8String any
different?
At least in the case of -fileSystemRepresentation, the docs do say that
the result is automatically freed, so my only memory-related doubt is
in connection with -UTF8String.
Thanks for any light that you can all shed on this one...
Cheers
Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.