Optional arguments in Objective-C 2.0?

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

提交回复
热议问题