简体   繁体   中英

Objective-C Private Property Inheritance

I just wanted to make sure my understanding of property inheritance was correct. I'm currently trying to make a subclass of a UIViewController. In my UIViewController all my outlets and such are declared in the implementation section like so:

@interface BaseClass()

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@end

Thus those properties are then private, correct? Now when I try and make a subclass and access those properties using my getters and setters, I cannot access them from my subclass. Is the proper form to redeclare those properties again in my subclass' implementation section, like so?

@interface SubClass()

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@end

I guess I am ok with doing it this way, but then I feel like it ruins the purpose of inheritance. What is the proper way/what am I doing wrong?

I would declare the property in the public interface for the BaseClass - I don't see any reason to put them in a class extension.

@interface BaseClass : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@end

@interface SubClass : BaseClass

// No need to redeclare the property as you're inheriting it.

@end

[EDIT]

If you must use the class extension then you could use a private header to achieve the same.

Public header for BaseClass (BaseClass.h)

@interface BaseClass : UIViewController

@end

Private header for BaseClass (BaseClass-Private.h)

@interface BaseClass ()

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@end

Public header for SubClass (SubClass.h)

#import "SubClass.h"

@interface SubClass : BaseClass

@end

Implementation of SubClass (SubClass.m)

#import "BaseClass-Private.h"

@implementation SubClass

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM