Saraswati Saraswati - 1 year ago 271
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download