Objective-c – casting a NSString to char problem

iphoneobjective-c

i want to casting my NSString to a constant char
the code is shown below :

NSString *date = @"12/9/2009";
char datechar = [date UTF8String]

NSLog(@"%@",datechar);

however it return the warning
assignment makes integer from pointer without a cast
and cannot print the char properly,can somebody tell me what is the problem

Best Solution

Try something more like this:

NSString* date = @"12/9/2009";
char* datechar = [date UTF8String];
NSLog(@"%s", datechar);

You need to use char* instead of char, and you have to print C strings using %s not %@ (%@ is for objective-c id types only).

Related Question