Optional arguments in Objective-C 2.0?

前端 未结 5 602
旧巷少年郎
旧巷少年郎 2020-12-04 21:04

In Objective-C 2.0, is it possible to make a method where the argument is optional? That is, you can have a method call like this:

[aFraction print];
         


        
5条回答
  •  情话喂你
    2020-12-04 21:39

    Slightly related you can have optional arguments. Meaning you can call a method with any number of arguments if you like. But that is a feature from C (varargs).

    An example, is the NSArray message:

    + (id)arrayWithObjects:(id)firstObj, ...
    

    Example of usage:

    NSArray *myArray;
    NSDate *aDate = [NSDate distantFuture];
    NSValue *aValue = [NSNumber numberWithInt:5];
    NSString *aString = @"a string";
    
    myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
    

    This is straight from the Apple docs. You indicate the end of all your arguments with a nil.

提交回复
热议问题