How do you define a method in your own classes that accepts an NSString with format?
I see several different things using it like NSLog
, [NSPredicate predicateWithFormat:(NSString *)predicateFormat, ...]
and of course [NSString stringWithFormat:(NSString *)format, ...]
Also, in the header files, NSLog and stringWithFormat have the following after their declaration:
NS_FORMAT_FUNCTION(1,2)
. Googling didn't help much with telling me what this meant.
Obviously the ellipsis is the format parameters, but I don't know how to deal with them in the method itself.
You need to make use of some C-code:
- (void)someMethod:(NSString *)format,... {
va_list argList;
va_start(argList, format);
while (format) {
// do something with format which now has next argument value
format = va_arg(argList, id);
}
va_end(argList);
}
And it's possible to forward the args in to NSString
as follows:
- (void)someMethod:(NSString *)format,... {
va_list args;
va_start(args, format);
NSString *msg = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With