Saraswati Saraswati - 7 months ago 102
iOS Question

convert char * to NSString

I want to show the char* in the UITextField


char *data;
char *name=data+6;
txtName.text=[[NSString alloc] initWithCString:name encoding:NSUTF8StringEncoding];

but I am not getting the correct value.


To create an NSString from a const char *, simply use these methods:

Returns an autoreleased object:

[NSString stringWithUTF8String:name];

Returns a retaind object:

[NSString initWithUTF8String:name];


If you are not getting the correct value, then something is wrong with the data. Add a few NSLog calls to see what the strings contain.