Optional arguments in Objective-C 2.0?

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

提交回复
热议问题