Wrong argument type for NSAssert1
-
24-06-2021 - |
Domanda
This isn't giving me an error but several warnings I'd like to get rid of.
OSErr err = AudioUnitInitialize(toneUnit);
NSAssert1(err == noErr, @"Error initializing unit: %ld", err);
The warning I'm getting is: Format specifies type 'long' but the argument has type 'OSErr' (aka 'short')
. Obviously I can change this by using long
instead of OSErr
but I was wondering why I'm getting the error. This is an upcast so I thought it shouldn't cause any problems.
Can anyone explain?
Soluzione
Since NSAssert1
is a variadic function implemented within the constraints of C, it cannot perform the cast internally: it is not possible to find the type of an argument passed in the vararg section of the function. Objective-C compiler will perform the default type promotions before making the call, but the short
is converted to int
, not to long
by default. That is why you should either insert an explicit cast, or use a different format specifier.
Altri suggerimenti
Use
NSAssert1(err == noErr, @"Error initializing unit: %hd", err);
or
NSAssert1(err == noErr, @"Error initializing unit: %ld", (long)err);
instead.