Optional arguments in Objective-C 2.0?

前端 未结 5 596
旧巷少年郎
旧巷少年郎 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:36

    To avoid the Parse issue: 'aVariable' used as the name of the previous parameter rather than as part of the selector you get from the compiler as a warning you should do:

    - (void)printWithParameter:(BOOL)sv color:(NSColor *)color{
       // your cool code goes here
       if ( sv ) {
          NSLog(@"cool stuff turned on");
       }
       else {
          NSLog(@"cool stuff turned off");
       }
    }
    

    and you could call the method, such:

    [self printWithParameter:NO color:[UIColor someColor]] // NO instead of 0
    

    or

    [self printWithParameter:YES color:[UIColor someColor]] // YES instead of 1
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 21:45

    You can declare multiple methods:

    - (void)print;
    - (void)printWithParameter:(id)parameter;
    - (void)printWithParameter:(id)parameter andColor:(NSColor *)color;
    

    In the implementation you can do this:

    - (void)print {
        [self printWithParameter:nil];
    }
    
    - (void)printWithParameter:(id)parameter {
        [self printWithParameter:nil andColor:[NSColor blackColor]];
    }
    

    Edit:

    Please do not use print and print: at the same time. First of all, it doesn't indicate what the parameter is and secondly no one (ab)uses Objective-C this way. Most frameworks have very clear method names, this is one thing that makes Objective-C so pain-free to program with. A normal developer doesn't expect this kind of methods.

    There are better names, for example:

    - (void)printUsingBold:(BOOL)bold;
    - (void)printHavingAllThatCoolStuffTurnedOn:(BOOL)coolStuff;
    
    0 讨论(0)
  • 2020-12-04 21:51

    Another option for multiple optional arguments is to pass them all in an NSDictionary:

    - (void)someMethodWithOptions:(NSDictionary *)options;
    

    then declare that the options dictionary may contain:

    - key1, value must be a BlahObject
    - key2, value must be a NonBlahObject
    

    you can then allow any of the keys to be absent and even the dictionary itself could be nil.

    0 讨论(0)
  • 2020-12-04 21:57

    The way you did it is the right way to do it in Objective-C. It's used extensively in Cocoa itself. For example, some of NSString's initializers:

    – initWithFormat:  
    – initWithFormat:arguments:  
    – initWithFormat:locale:  
    – initWithFormat:locale:arguments:
    

    The reason it works is because the : is part of the method name, so as far as the compiler is concerned, print and print: are completely different messages that are no more closely connected than "print" and "sprint".

    However, the particular names of the methods you gave aren't a very good case for this, because it's unclear from the name what the parameter is (or what "print" by itself means if the parameter is what the object prints). It would be better to have, say, printFalseMessage and printMessageWithFlag:.

    0 讨论(0)
提交回复
热议问题