objective-cxcodeintegernslogformat-specifiers

NSLog cannot print negative int with %d in Xcode 15


I'm coding Objective C in Xcode 15 and found this NSLog %d problem. The code I used to test:

    int someInt = -1;
    NSLog(@"AppDelegate status: %d (%d) ((%.0f)) %@ -> %d)" ,
          someInt,
          -1,
          someInt * 1.0,
          [NSString stringWithFormat:@"%d", someInt],
          [NSString stringWithFormat:@"%d", someInt].intValue);

The result in Log:

AppDelegate status: 18,446,744,073,709,551,615 (18,446,744,073,709,551,615) ((-1.0000)) -1 -> 18,446,744,073,709,551,615)

So for -1,

  1. it's printed as 8,446,744,073,709,551,615 with %d
  2. Converted to float using %f shows correct -1.0000
  3. Converted to NSString shows correct -1

But

  1. %d with -1 doesn't work
  2. %d with someInt doesn't work
  3. %d with NSString converted back to intValue doesn't work.

Seems like NSLog is printing %d as a unsigned int. In addition, using printf is fine with -1 and %d.

Is there some setting problem of my Xcode? Any ideas how to fix this?


Solution

  • From Xcode 15 Release Notes:

    Console

    New Features

    By default, Xcode streams os_logs through the unified logging and activity tracing infrastructure. The output may be formatted differently compared to previous versions of Xcode, and its order relative to standard IO may also change. To customize the behavior of logging, edit the Run scheme action to set the environment variable IDELogRedirectionPolicy. The value “oslogToStdio” redirects os_log messages to standard IO and formats them in a style identical to previous versions of Xcode. The value stdioToOSLog redirects standard IO to the os_log messages, and presents them in the debug console with additional metadata. (109380695)

    Apparently there's a bug in the new logging system. Workaround: Edit Scheme and add environment variable IDELogRedirectionPolicy with value oslogToStdio.