Search code examples
objective-cnslog

Why does NSLog display incorrect int and char?


I think I'm doing everything correctly, but NSLog output doesn't match the correct values shown by hovering over the variables. Synthesis and dot notation are working correctly.
Hovering over all variables reveals correct values, as shown in // comments. But NSlog displays incorrectly in the Debugger console; output is also shown in the // comments.

In test, an instance of Screen Class:

{   int i;
    char j;
}

In 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= ,

What am I missing? Or is this a genuine bug in Xcode 3.2.4, OSX 10.6?


Solution

  • You've closed the parentheses for NSlog() before passing the format arguments. Change this:

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

    to this:

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

    Aside from separating arguments in function calls, commas can also be used to separate statements. Your attempt is equivalent to:

    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.