Objective-C - Private vs Protected vs Public

前端 未结 4 578
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 16:53

I am hoping for some clarification on how Private vs Protected vs Public works with respect to class members when programming in Objective-C - I thought I knew the differenc

4条回答
  •  甜味超标
    2020-12-07 17:18

    The usual trick is to create a class extension inside the .m file and put your private/protected property there instead of in the header.

    //Person.m
    
    @interface Person()
    
    @property float height
    
    @end
    

    this hides the 'height' property

    Another trick is if you want to create a readonly property is to declare it in the header as

    @property(readonly) int myproperty
    

    but in the class extension as readwrite which allows your .m to modify the value using the getter/setter

    @property(readwrite) int myproperty
    

提交回复
热议问题