【问题标题】:Why does NSLog display incorrect int and char?为什么 NSLog 显示不正确的 int 和 char?
【发布时间】:2012-11-28 00:33:31
【问题描述】:

我认为我所做的一切都是正确的,但是 NSLog 输出与将鼠标悬停在变量上显示的正确值不匹配。合成和点符号工作正常。
将鼠标悬停在所有变量上会显示正确的值,如 // cmets.但是 NSlog 在 Debugger 控制台中显示不正确;输出也显示在 // cmets.

在测试中,Screen Class 的一个实例:

{   int i;
    char j;
}

在 AppDelegate 中:

test.i = 10;    // hover shows 10        OK
test.j = 'z';   // hover shows 122 'z'   OK
NSLog(@"i= %i, j= %c"),test.i, test.j;// hover shows 10,122 'z'OK  
but Debugger Console shows  i= 2097168, j= $  


int k = 10; // hover shows 10        OK
char l = 'z';   // hover shows 122 'z'   OK
NSLog(@"k= %i, l= %c"),k, l;          // hover shows 10,122 'z'OK  
but Debugger Console shows  k= 6055, l= ,

我错过了什么?或者这是 Xcode 3.2.4、OSX 10.6 中的真正错误?

【问题讨论】:

    标签: objective-c nslog


    【解决方案1】:

    在传递格式参数之前,您已经关闭了 NSlog() 的括号。改变这个:

    NSLog(@"i= %i, j= %c"),test.i, test.j;
    

    到这里:

    NSLog(@"i= %i, j= %c",test.i, test.j);
    

    除了在函数调用中分隔参数外,逗号还可以用于分隔语句。您的尝试相当于:

    NSLog(@"i= %i, j= %c"); // An NSLog with no args, will print garbage.
    test.i; // A valid, though pointless, statement.
    test.j; // A valid, though pointless, statement.
    

    【讨论】:

    • 非常感谢。我一遍又一遍地看着它,没有看到我的错误。现在完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多