NSString returns nonsense

Totally lost with this. Here is my code:

theColor = [NSString stringWithFormat:@"white"]; NSLog(@"%s", theColor); 

What is saved:

 †t†Γ₯ 

I have to do something stupid, but I can’t understand it for me.

+4
source share
3 answers

Change the print as follows:

 NSLog(@"%@", theColor); 

Hope this helps.

The fact is that %s expects an array of C-string (char with a NULL terminator), and you pass an instance of NSString that does not match the C-string. The modifier that you need in the format for printing NSString content, %@ .

+8
source

%s is for printing C-style strings

%@ is for printing Objective-C objects (e.g. NSString).

+4
source

BTW: "theColor = [NSString stringWithFormat: @" white "];" - why not "theColor = @" white ";"?

Hi

0
source

All Articles