这个问题让我完全迷失了。这是我的代码:
theColor = [NSString stringWithFormat:@"white"];
NSLog(@"%s", theColor);
返回的内容是:
†t†å
我一定做了什么愚蠢的事情,但就是想不明白。
这个问题让我完全迷失了。这是我的代码:
theColor = [NSString stringWithFormat:@"white"];
NSLog(@"%s", theColor);
返回的内容是:
†t†å
我一定做了什么愚蠢的事情,但就是想不明白。
将您的打印更改为:
NSLog(@"%@", theColor);
希望能对您有所帮助。
问题在于%s
期望一个C字符串(带有NULL终止符的char数组),而你正在传递一个NSString
实例,它与C字符串不同。在格式化输出NSString
内容时需要使用的修饰符是%@
。
%s
是用于打印C风格的字符串。
%@
是用于打印Objective-C对象(如NSString)的。
顺便问一下:“theColor = [NSString stringWithFormat:@"white"];” - 为什么不用“theColor = @"white";”呢?
祝好