为什么我必须对@synthesize使用“受保护的”属性?

时间:2019-11-18 08:43:31

标签: objective-c inheritance class-extensions

我有三节课:
AbstractRenderElement
PlaneRenderElement
ButtonRenderElement

Button从Plane继承而Plane从Abstract继承。
我有五个文件:
AbstractRenderElement.h
PlaneRenderElement.h
ButtonRenderElement.h
AbstractRenderElement+Protected.h
PlaneRenderElement+Protected.h

+Protected.h文件包含一个Class扩展名,该扩展名被导入到该类本身及其直接子类的.m文件中。
AbstractRenderElement+Protected.h导入到AbstractRenderElement.mPlaneRenderElement.m中,并且
PlaneRenderElement+Protected.h被导入到PlaneRenderElement.mButtonRenderElement.m中。

现在我的问题是: PlaneRenderElement定义了两个属性: 类扩展中的WidthHeight(因为它是受保护的)。 即使我在Width的类扩展中定义了HeightPlane的访问方式,为什么我仍要在Button的实现中进行合成?

这是我的文件:
AbstractRenderElement.h

@interface AbstractRenderElement : NSObject <RenderElement>
@property(nonatomic, readonly) float* modelMatrix;
@property(nonatomic, readonly) float* viewMatrix;
@property(nonatomic, readonly) float* projectionMatrix;
@property(nonatomic, readonly) float* targetModelMatrix;
@property(nonatomic, readonly, strong) Transform* transform;
//initmethod here
@end

AbstractRenderElement+Protected.h

@interface AbstractRenderElement ()
@property(nonatomic, readwrite) float* modelMatrix;
@property(nonatomic, readwrite) float* viewMatrix;
@property(nonatomic, readwrite) float* projectionMatrix;
@property(nonatomic, readwrite) float* targetModelMatrix;
@property(nonatomic, readwrite) BOOL readyForRendering;
@property(nonatomic, readwrite) MetalRenderData* renderData;
@property(nonatomic, readwrite, strong) Transform* transform;
@end

AbstractRenderElement.mm

#import "AbstractRenderElement.h"
#import "AbstractRenderElement+Protected.h"

@implementation AbstractRenderElement
//initializers, methods etc.
@end

PlaneRenderElement.h

@interface PlaneRenderElement : AbstractRenderElement
@property(nonatomic, readonly) float width;
@property(nonatomic, readonly) float height;
@end

PlaneRenderElement+Protected.h

@interface PlaneRenderElement ()
@property(nonatomic, readwrite) float width;
@property(nonatomic, readwrite) float height;
@end

PlaneRenderElement.mm

#import "PlaneRenderElement.h"
#import "PlaneRenderElement+Protected.h"

@implementation PlaneRenderElement
//initializers, methods etc.
@end

ButtonRenderElement.h

#import <Foundation/Foundation.h>
#import "PlaneRenderElement.h"

@interface RenderButtonV0 : PlaneRenderElement
@end

ButtonRenderElement.mm

#import "ButtonRenderElement.h"
#import "PlaneRenderElement+Protected.h"

@interface RenderButtonV0 ()
//Private Properties of Button
@end

@implementation RenderButtonV0
-(instancetype)initWithButton:(ButtonV0*)button {
    self = [super init];
    if (self) {
        [self setButton:button];
        [self setWidth:[[self button] width] / 50.0f]; //crash, unrecognized selector
        [self setHeight:[[self button] height] / 50.0f];
    }
    return self;
}
//Rest of Implementation

1 个答案:

答案 0 :(得分:0)

您缺少在实现文件中导入私有属性,因此编译器不会为您合成它们。

您在#import AbstractRenderElement+Protected.hAbstractRenderElement.m中应该有#import PlaneRenderElement+Protected.h in PlaneRenderElement.m

基本规则:您应始终在该类的@implementation文件中导入该类的扩展名。