Re: cString deprecated!
Re: cString deprecated!
- Subject: Re: cString deprecated!
- From: Douglas Davidson <email@hidden>
- Date: Tue, 3 Sep 2002 10:02:30 -0700
As Matt's examples make clear, it is easy for you to provide
convenience methods that will serve for whatever your particular needs
happen to be.
One thing worth noting is that NSFileManager already provides methods
for converting to and from C strings in the file system encoding.
These methods should be used for any string that may represent a
filesystem name or path being sent to or received from the BSD layer,
either directly or via the convenience APIs for this on NSString, e.g.
- (const char *)fileSystemRepresentation;
For other usages UTF8String is often suitable.
Douglas Davidson
On Tuesday, September 3, 2002, at 09:20 AM, Matt Neuburg wrote:
On Mon, 2 Sep 2002 17:30:28 -0400, Owen Anderson <email@hidden>
said:
Is it just me, or does this seem like a perfectly rotten idea? the
cString method is extremely useful when porting things from UNIX, or
when writing wrappers around UNIX tools. I see their point about
encodings, but there still needs to be some easy to use way to convert
NSStrings to c strings and vice-versa.
Owen Anderson
On Monday, September 2, 2002, at 05:17 PM, Charles Srstka wrote:
Expect -[NSString cString] method to be deprecated and removed from
public API at some point in the near future
I had noticed the deprecation and have already removed cString calls
from
my apps. It is perfectly true that the assumption made by cString,
that the
current system default encoding should be used, is vile and dangerous,
and
it was already causing me trouble. Therefore this deprecation is a good
thing.
It is also true that in the absence of a simple call allowing you to
convert a C string to an NSString while specifying the encoding, this
deprecation is really annoying. I can't believe that Apple didn't
supply us
with one. However, it's easy to write one. Here is one way:
- (const char*) myCString: (int) encoding {
return [[[self stringByAppendingString:
@"\0"] dataUsingEncoding: encoding
allowLossyConversion:YES]
bytes];
}
Note that although the docs are not so clear about this, you must also
write your own stringWithCString, because this makes the same bad
assumption. Here is an example:
+ (id) myStringWithCString: (const char *) cString encoding: (int)
encoding{
NSData* d = [NSData dataWithBytesNoCopy:(void*)cString
length:strlen(cString) freeWhenDone:NO];
return [[[NSString alloc] initWithData: d encoding: encoding]
autorelease];
}
m.
--
matt neuburg, phd = email@hidden, http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
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.
_______________________________________________
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.