Objective-C - Private vs Protected vs Public

前端 未结 4 571
爱一瞬间的悲伤
爱一瞬间的悲伤 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:14

    You are setting the visibilities of the ivars, not the properties. Your properties generate public getter and setter methods.

    To make private properties, you can put the properties in a private category in the .m file.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-07 17:22

    visibility does not affect methods. methods are as good as public when visible to clients (and potential pitfalls/bugs when invisible to clients). instead, visibility affects instance variables. try this:

    #import <Foundation/Foundation.h>
    #import "Person.h"
    #import "Man.h"
    
    
        int main (int argc, const char * argv[]) 
            {
                NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
                //Create a Person object
                Person * aPerson = [[Person alloc]init];
    
                //Create a Man object
                Man * aMan = [[Man alloc]init];
    
    
                //Let's attempt to modify our Person class members
                aPerson->height = 5.11; //Protected
                aPerson->age = 21; //Protected
                aPerson->yob = 2010; //Private
                aPerson->alive = YES; //Public
    
    
                //Let's now attempt to modify the same members via our
                //derived class Man - in theory, the private members should
                //not be accessible by the derived class man
                aMan->height = 6; //Protected
                aMan->age = 26; //Protected
                aMan->yob = 2011; //Private
                aMan->alive = YES; //Public
                aMan->mWeight = 190; //Protected member of Man Class
    
    
    
                [pool drain];
                return 0;
            }
    

    this prevents the subclasses from accessing ivars directly -- forcing them and clients to use the accessors (if provided).

    this is all a bit weak because categories allow clients to overcome this.

    also, older 32 bit objc programs did't really check that the visibility was declared correctly. fortunately, that's been deprecated in 32 and an error in 64.

    if you really want something to be private to subclasses and categories, use PIMPL with an unpublished/opaque type.

    method visibility (as found in Java, C++, etc.) is a feature i'd use in objc.

    0 讨论(0)
  • 2020-12-07 17:25

    You aren't accessing the members - you are accessing the property on Person which does not have the access level specified.

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