Suppose i have a method that returns BOOL
like :
+(BOOL) isNewCar {
//returns YES or NO
}
and then I use this method in NSLog
as :
NSLog("Is my car new, the answer is %@", [MyUtil isNewCar]);
Then i get compiler warning as :
If i ignore the warning and run, the app crashes.
Now I have another method called printCar
which accepts variable arguments just like NSLog
whose implementation is :
-(void) printCar:(NSString *)msg, ... {
NSString * contents;
va_list args;
va_start(args, msg);
contents = [[NSString alloc] initWithFormat:msg arguments:args];
va_end(args);
[self writeToFile:contents];
}
When i use the above method as :
[self printCar:@"The car is mine or not : %@", [MyUtil isNewCar]];
the compiler doesn't generate a warning. Also, the app crashes.
Question : Is there a way to generate a warning if wrong format specifiers are given to our custom method just as NSLog
does??
Thanks in advance!
Yes , you need NS_FORMAT_FUNCTION
macro to do that , if you press Command and then click on NSLog
method , you can also see it
void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2)
so your method
-(void) printCar:(NSString *)msg, ...
becomes
-(void) printCar:(NSString *)msg, ... NS_FORMAT_FUNCTION(1,2)
you can also Command Click NS_FORMAT_FUNCTION
to see more details about it