• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: cString deprecated!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cString deprecated!


  • Subject: Re: cString deprecated!
  • From: Matt Neuburg <email@hidden>
  • Date: Tue, 3 Sep 2002 09:20:27 -0700

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.

  • Follow-Ups:
    • Re: cString deprecated!
      • From: Julien Jalon <email@hidden>
    • Re: cString deprecated!
      • From: Rosyna <email@hidden>
    • Re: cString deprecated!
      • From: Kris Amico <email@hidden>
    • Re: cString deprecated!
      • From: Douglas Davidson <email@hidden>
  • Prev by Date: Re: The macs serial number (clearer)
  • Next by Date: Re: TM character blows array out of water
  • Previous by thread: Re: iso-8859-1 over UTF8 (was: Re: cString deprecated!)
  • Next by thread: Re: cString deprecated!
  • Index(es):
    • Date
    • Thread