代码之家  ›  专栏  ›  技术社区  ›  Ian McIntyre Silber

nsstring返回jibberish

  •  4
  • Ian McIntyre Silber  · 技术社区  · 14 年前

    完全失去了这个。以下是我的代码:

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

    它正在修复:

    †t†å
    

    我一定做了件蠢事,但我一辈子都搞不清楚。

    3 回复  |  直到 14 年前
        1
  •  8
  •   Pablo Santa Cruz    14 年前

    将打印更改为:

    NSLog(@"%@", theColor);
    

    希望它有帮助。

    问题是 %s 需要一个C字符串(带有空终止符的char数组),而您正在传递 NSString 实例与C字符串不同。要打印的格式中所需的修饰符 非字符串 内容是 %@ .

        2
  •  4
  •   Jon Reid    14 年前

    %s 用于打印C样式字符串。

    %@ 用于打印Objective-C对象(如nsstring)。

        3
  •  0
  •   Objective Interested Person    14 年前

    btw:_156;thecolor=[nsstring stringwithformat:@“white”];_ _“为什么不__thecolor=@“white”;_?

    问候语