Aadil
Aadil

Reputation: 703

Converting NSString into const char

I have an String and i want to convert it into Const Char . I Had tried but it's not Working

NSString *str4 = [NSString stringWithUTF8String:character];

Upvotes: 19

Views: 17984

Answers (3)

Rams
Rams

Reputation: 1751

NSString *myString = @"Hello";  

const char *cString = [myString cStringUsingEncoding:NSASCIIStringEncoding];

or

const char * cstr2 = [ myString UTF8String ];

Upvotes: 2

Vipin
Vipin

Reputation: 4728

Do like this:

NSString *strSQL = [[NSString alloc]init];

then something in your *strSQL

then this statement:

const char *bar = [strSQL UTF8String]; 

Upvotes: 48

gsempe
gsempe

Reputation: 5499

You should use cStringUsingEncoding: method
Take a look at this method here

Upvotes: 1

Related Questions