When is it advisable to use variable arguments in an Objective-C method?

When is it advisable to write a method with a variable number of arguments (e.g. NSString +stringWithFormat:)?

A brief search for variational methods from Apple, apparently, includes only two classes: when creating a data structure (NSArray +arrayWithObjects:, NSSet +setWithObjects:)or when formatting a string (NSString +stringWithFormat:, NSPredicate +predicateWithFormat:).

Apple documentation for variable methods includes an example that differs significantly from the previously mentioned data structure methods, but is still in the same camp.

Is it appropriate to use variational methods in any other context? Is Apple

+3
source share
2 answers

(, NSString + stringWithFormat:)?

printf -like, . / .

Apple, -, : (NSArray + arrayWithObjects:, NSSet + setWithObjects:) (NSString + stringWithFormat:, NSPredicate + predicateWithFormat:).

nil. , nil . , (new-ish) Objective-C ; , count , nil.


?

C, . , , API.

, API. API, va_list.

, .

+2

, Cocoa . , NSLog NSAssert , .

, .

, API , :

NSNumber *res = [Evaluator evalExpression:@"%1 + %2 * %3", @10, @20, @5];
// returns @110

API XML, :

MyXmlTree *tree = [MyXmlTree addElementWithTag:@"root" andChildren:
    [MyXmlTree elementWithTag:@"hello"]
,   [MyXmlTree elementWithTag:@"world"]
,   nil];

- (.. , ).

+1

All Articles