The initWithObjects: method of NSArray takes an indefinite list of arguments:
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:(id), ..., nil
How can I define my own method like this?
- (void)CustomMethod:????? <= want to take infinite arguments {
}
The “infinite arguments” are variable arguments and the methods that use them are called variadic methods. You define them the same way as your
NSMutableArrayexample. Apple’s Technical Q&A has an example of how to implement it.The reason for the
nilargument is so that you know when you have reached the end of the list. Functions likeNSLogandprintfdo not require the last argument to benilbecause it can count the number of specifiers in the format string (%d,%setc…)