objective-cc-preprocessoriphone-64bit

Removing NSInteger warning using __LINE__


I have a log function:

#define LOGERROR(err) if(err) {                     \
    LOGTRACE(@"[NSError] %s (%d): (%d:%@) Reason: %@",  \
        __PRETTY_FUNCTION__,                            \
        __LINE__,                                       \
        err.code,                                       \
        err.domain,                                     \
        err.localizedDescription)                       \
}

When I call it with

LOGERROR(playerItem.error);

I get a warning: "NSInteger should not be used as a format argument, add an explicit cast to long".

Xcode's autofix inserts %ld before LOGERROR, which is wrong.

I think this warning comes from the use of __LINE__, which according to https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html returns an int.

How can I remove the warning from this call?


Solution

  • Just to formalize the answer I gave in a comment:

    The compiler is complaining about the use of err.code, which returns an NSInteger. Cast it to a long: (long)(err.code), and then use %ld in the format string.