Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion NSString to const char string[]

I have one query related conversion I have one NSString object with content some string like below

NSString *string = @"do nothing";

Now I am trying to pass string in below code like

const char inputString[] =  string;

Xcode showing error

Array initializer must be an initializer list or string literal.

Is there any workaround for the same.

Thanks in advance,

like image 646
milanjansari Avatar asked Aug 01 '26 21:08

milanjansari


2 Answers

The simpliest is to use UTF-8 conversion (which replaces the old and long deprecated - (const char *)cString method):

const char *Cstr = myObjCStr.UTF8String;
like image 142
Nicolas Buquet Avatar answered Aug 03 '26 12:08

Nicolas Buquet


Try this

NSString *string = @"do nothing";
const char * inputString = [string UTF8String];

If you want to use other encoding,use this cStringUsingEncoding

like image 23
Leo Avatar answered Aug 03 '26 13:08

Leo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!