A string with the format as an argument to the method (objective-c)

[NSString stringWithFormat:]; can take several arguments, even if it is declared as an NSString, not an NSArray, and there is only one colon.

How to do this for my own method, which is similar to replacing NSLog, which writes to a text field, so it is often used, and I do not want to add more square brackets.

+4
source share
1 answer

Use the ellipsis after the name of your argument:

  (NSNumber *) addValues:(int) count, ...; 

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocDefiningClasses.html

Then you need to use va_list and va_start to iterate through the provided arguments:

 - (NSNumber *) addValues:(int) count, ... { va_list args; va_start(args, count); NSNumber *value; double retval; for( int i = 0; i < count; i++ ) { value = va_arg(args, NSNumber *); retval += [value doubleValue]; } va_end(args); return [NSNumber numberWithDouble:retval]; } 

Example from: http://numbergrinder.com/node/35

Note that this is C built-in functionality, not part of Objective-C; There is a good explanation for using va_arg here:

http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html

+2
source

All Articles