[NSString characterAtIndex:] -> [NSString stringWithCString:encoding:]
[NSString characterAtIndex:] -> [NSString stringWithCString:encoding:]
- Subject: [NSString characterAtIndex:] -> [NSString stringWithCString:encoding:]
- From: Gabriel Fernandez <email@hidden>
- Date: Mon, 1 Mar 2010 12:25:05 -0500
Hi, I'm having trouble with NSString - I'm capturing NSTextField edits and analyzing the typed (uni-)chars:
{
int bufsize = 3*sizeof(unichar) ;
static unichar *buf = nil ;
if( nil == buf )
{
buf = (unichar*) malloc( bufsize ) ;
if( nil != buf )
{
buf[ 2 ] = 0 ;
} else {
NSLog(@"textDidChange: malloc error");
return ;
}
}
NSString * s = [textField stringValue] ;
// could put this in a loop, but I'm only handling 2 characters, so...
if( 0 < [s length] )
{
buf[0] = [s characterAtIndex:0];
buf[1] = 0 ;
}
if( 1 < [s length] )
{
buf[1] = [s characterAtIndex:1];
}
// buf is set to the character string properly...
s = [NSString stringWithCString:(const char *)buf encoding: NSUnicodeStringEncoding] ;
NSInteger s_len = [s length]; //s_len is always zero
}
if I change the encoding: to ascii, I get one character in the new NSString, as I'd expect, but with Unicode encoding, I get no characters - WHY?!
Thanks In Advance,
Gabe
----------------------------------------------------------------
Gabriel Fernandez
Wheel Software
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden