Good practice for disambiguating argument names versus instance variable names in Objective-C

前端 未结 3 1792
花落未央
花落未央 2021-01-13 00:57

I run into a fairly common scenario in Objective-C where I pass in a variable to an init method and then want to assign it to an instance variable of the same name. However

相关标签:
3条回答
  • 2021-01-13 01:21

    For setters (and by extension initializers), I believe the convention is to prefix the parameter name with new:

    - (void)setCrunk:(Crunk *)newCrunk;
    - (id)initWithCrunk:(Crunk *)newCrunk;
    

    In general, I think the most common form I've seen is to call the parameter theCrunk, but Apple seems to recommend aCrunk.

    0 讨论(0)
  • 2021-01-13 01:29

    And changing the name to "inValue" is not a good idea? What you have here - your 'solution' is complex, especially with the accessors, etc of Obj-C 2. Since self.value and inValue are different things, they need different names.

    Note that you can use

    -(void)method1:(NSString*)value; 
    

    in the header

    and

    -(void)method1:(NSString*)inValue; 
    

    in the .m file.

    0 讨论(0)
  • 2021-01-13 01:35

    If you use only 1 the compiler will give you a warning.

    You can combine 1 & 2 by using :

    @synthesize value = _value;
    

    If you want to hide your variable from the inheritors you can declare a empty named category and declare your property there.

    For 3 you can use aValue for your argument.

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