Saraswati Saraswati - 3 months ago 26
iOS Question

convert char * to NSString

I want to show the char* in the UITextField

situation:

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


but I am not getting the correct value.

Answer

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];

Reference.

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.