Difference between _property and self.property

前端 未结 3 511
轻奢々
轻奢々 2020-12-19 21:22

I\'m slightly confused as to the proper conventions when dealing with properties. I\'ll illustrate my question through an example. So from the example below I know that func

3条回答
  •  别那么骄傲
    2020-12-19 21:45

    When you write out @synthesize loan = _loan; it is basically shorthand to say that what you are synthesizing is = to a private member it would be similar to the verbose way of coding it below:

    //ClassA.h
    @interface ClassA: UIViewController {
        double _loan;
    }
    
    @property double loan;
    @end
    
    //ClassA.m
    @implementation ClassA
    @synthesize loan = _loan;
    

    It is good practice to write it out this way so you can clearly have a '_' to signify your private members within your class code but when exposing this as a property to other classes that shouldn't be shown.

提交回复
热议问题