开发者

NSString returning jibberish

开发者 https://www.devze.com 2023-01-10 03:47 出处:网络
Totally lost with this one. Here\'s my code: theColo开发者_C百科r = [NSString stringWithFormat:@\"white\"];

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

theColo开发者_C百科r = [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.


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 %@.


%s is for printing C-style strings.

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


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

Greetings

0

精彩评论

暂无评论...
验证码 换一张
取 消