override property from superclass in subclass

前端 未结 2 981
深忆病人
深忆病人 2021-01-06 15:37

I want to override an NSString property declared in a superclass. When I try to do it using the default ivar, which uses the the same name as the property but with an unders

2条回答
  •  梦谈多话
    2021-01-06 15:58

    to access the superclass variables, they must be marked as @protected, access to such variables will be only inside the class and its heirs

    @interface ObjectA : NSObject
    {
        @protected NSObject *_myProperty;
    }
    @property (nonatomic, strong, readonly) NSObject *myProperty;
    @end
    
    @interface ObjectB : ObjectA
    @end
    
    @implementation ObjectA
    @synthesize myProperty = _myProperty;
    @end
    
    @implementation ObjectB
    - (id)init
    {
        self = [super init];
        if (self){
            _myProperty = [NSObject new];
        }
        return self;
    }
    @end
    

提交回复
热议问题