Ian McIntyre Silber
Ian McIntyre Silber

Reputation: 5663

NSString returning jibberish

Totally lost with this one. Here's my code:

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

Which is returing:

†t†å

I must be doing something stupid, but can not figure it out for the life of me.

Upvotes: 4

Views: 96

Answers (3)

BTW: “theColor = [NSString stringWithFormat:@"white"];” – why not “theColor = @"white";”?

Greetings

Upvotes: 0

Jon Reid
Jon Reid

Reputation: 20980

%s is for printing C-style strings.

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

Upvotes: 4

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

Change your print to:

NSLog(@"%@", theColor);

Hope it helps.

The thing is that %s expects a C-string (char array with a NULL terminator) and you are passing a NSString instance which is not the same as a C-string. The modifier you need in a format to print NSString content is %@.

Upvotes: 8

Related Questions