Re: getCString a good idea here?
Re: getCString a good idea here?
- Subject: Re: getCString a good idea here?
- From: Prachi Gauriar <email@hidden>
- Date: Sat, 12 Jul 2003 18:36:51 -0500
On Saturday, July 12, 2003, at 5:30 PM, Michael Hanna wrote:
Hi I'm trying to output to the stdout with a foundation tool. But it
seems I'm not using getCString improperly.. I'm certain that the array
returns an NSString. phrase_01 is an NSArray:
srandom( (unsigned int) time(NULL)); // generate a seed value
NSLog ( @"phrase_01 = %@", phrase_01);
printf("I met her %s %s\n",
[[phrase_01 objectAtIndex: randPhrase( [phrase_01 count] ) ]
getCString],
[phrase_02 objectAtIndex: randPhrase( [phrase_02 count] ) ]);
<snip>
2003-07-12 18:24:34.769 countrysong[25224] *** -[NSConstantString
getCString]: selector not recognized
2003-07-12 18:24:34.770 countrysong[25224] *** Uncaught exception:
<NSInvalidArgumentException> *** -[NSConstantString getCString]:
selector not recognized
Basically, this means that getCString isn't a method for
NSConstantString. So your problem isn't that you're using getCString
improperly, it's that getCString doesn't exist. There is a method
called getCString: (notice the ':'), but that, like all Cocoa methods
that start with get... means that the method returns a value by
reference (which is not what you want), like
char *myCString;
[myString getCString:&myCString];
That doesn't seem like what you want to do.
You probably want to call cString, lossyCString, or (best) UTF8String.
cString returns a C string, but if it can't be converted without loss
of information, it raises an exception. lossyCString basically does
the same as cString except that if the string can't be represented
using the default C-string encoding, it loses the non-convertible info.
UTF8String returns an 8-bit lossless string.
In other words,
printf("I met her %s %s\n",
[[phrase_01 objectAtIndex: randPhrase( [phrase_01 count] ) ]
UTF8String],
[phrase_02 objectAtIndex: randPhrase( [phrase_02 count] ) ]);
HTH.
-Prachi
_______________________________________________
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.