在Objective-C继承中使用@ synthesize / @属性

时间:2022-09-07 10:15:46

If you have Class A with an instance var "foo" which has a @property/@synthesize directive, and Class B inherits from Class A, does it also need to @property/@synthesize "foo"? The reason I ask is because when I try to use Class B's "foo", the calling class says that "foo" is not something of a structured union or a member, which makes me believe it needs to be explicitly synthesized.

如果A类的实例var“foo”具有@ property / @ synthesize指令,而B类继承自A类,它是否还需要@ property / @ synthesize“foo”?我问的原因是因为当我尝试使用B类的“foo”时,调用类说“foo”不是结构化联合或成员的东西,这让我相信它需要明确合成。

5 个解决方案

#1


10  

No, you don't. Synthesized properties are added to class A and its subclasses automatically.

不,你没有。合成属性会自动添加到A类及其子类中。

#2


10  

If you have Class A with an instance var "foo" which has a @property/@synthesize directive, and Class B inherits from Class A, does it also need to @property/@synthesize "foo"?

如果A类的实例var“foo”具有@ property / @ synthesize指令,而B类继承自A类,它是否还需要@ property / @ synthesize“foo”?

No.

没有。

The reason I ask is because when I try to use Class B's "foo", the calling class says …

我问的原因是因为当我尝试使用B级的“foo”时,调用类说...

No, the compiler says it.

不,编译器说了。

… that "foo" is not something of a structured union or a member, which makes me believe it needs to be explicitly synthesized.

......“foo”不是结构化联盟或成员的东西,这让我相信它需要明确合成。

It is. In class A.

它是。在A班

The compiler is giving you that warning because it doesn't know about the @property, which is because you have neither declared it nor imported a header that declares it. You say that class A's header declares the property, so import class A's header into class B's implementation, so that the compiler knows about the property when compiling class B.

编译器给你这个警告,因为它不知道@property,这是因为你既没有声明它也没有导入声明它的头。你说类A的头部声明了属性,所以将类A的头部导入到B类的实现中,以便编译器在编译B类时知道该属性。

#3


4  

Just in case this helps someone.

以防这有助于某人。

I came across this problem too and read these answers and still couldn't access super class variables directly. They were declared as properties and synthesized in the super class and and I had imported the header into my subclass. I was stuck until I discovered I needed to declare the member variables in the @interface section in the super class as well as a property of the superclass....! e.g.

我也遇到了这个问题并阅读了这些答案,仍然无法直接访问超类变量。它们被声明为属性并在超类中合成,并且我已将标头导入到我的子类中。我被困住了,直到我发现我需要在超类中的@interface部分声明成员变量以及超类的属性....例如

@interface BuoyAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D coordinate;
    CLLocation* location;
    int type; 

}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) CLLocation* location;
@property (nonatomic, assign) int type;

#4


1  

WHen inheriting you should not need to redeclare any properties or variables.

继承你的时候不需要重新声明任何属性或变量。

Perhaps if you post your ClassB header file or a portion of then people can better pinpoint your problem.

也许如果你发布你的ClassB头文件或一部分人,可以更好地查明你的问题。

#5


0  

Correct, you just declare a @property outside of the typical member variable declaration curly brackets and then @synthesize the property in the .m file. I did notice that in the child class you have use self.propertyName to reference it but in the parent class you can just use the instant variable name.

更正,您只需在典型的成员变量声明大括号之外声明一个@property,然后@synthesize .m文件中的属性。我注意到在子类中你使用self.propertyName来引用它,但是在父类中你可以使用即时变量名。

#1


10  

No, you don't. Synthesized properties are added to class A and its subclasses automatically.

不,你没有。合成属性会自动添加到A类及其子类中。

#2


10  

If you have Class A with an instance var "foo" which has a @property/@synthesize directive, and Class B inherits from Class A, does it also need to @property/@synthesize "foo"?

如果A类的实例var“foo”具有@ property / @ synthesize指令,而B类继承自A类,它是否还需要@ property / @ synthesize“foo”?

No.

没有。

The reason I ask is because when I try to use Class B's "foo", the calling class says …

我问的原因是因为当我尝试使用B级的“foo”时,调用类说...

No, the compiler says it.

不,编译器说了。

… that "foo" is not something of a structured union or a member, which makes me believe it needs to be explicitly synthesized.

......“foo”不是结构化联盟或成员的东西,这让我相信它需要明确合成。

It is. In class A.

它是。在A班

The compiler is giving you that warning because it doesn't know about the @property, which is because you have neither declared it nor imported a header that declares it. You say that class A's header declares the property, so import class A's header into class B's implementation, so that the compiler knows about the property when compiling class B.

编译器给你这个警告,因为它不知道@property,这是因为你既没有声明它也没有导入声明它的头。你说类A的头部声明了属性,所以将类A的头部导入到B类的实现中,以便编译器在编译B类时知道该属性。

#3


4  

Just in case this helps someone.

以防这有助于某人。

I came across this problem too and read these answers and still couldn't access super class variables directly. They were declared as properties and synthesized in the super class and and I had imported the header into my subclass. I was stuck until I discovered I needed to declare the member variables in the @interface section in the super class as well as a property of the superclass....! e.g.

我也遇到了这个问题并阅读了这些答案,仍然无法直接访问超类变量。它们被声明为属性并在超类中合成,并且我已将标头导入到我的子类中。我被困住了,直到我发现我需要在超类中的@interface部分声明成员变量以及超类的属性....例如

@interface BuoyAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D coordinate;
    CLLocation* location;
    int type; 

}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) CLLocation* location;
@property (nonatomic, assign) int type;

#4


1  

WHen inheriting you should not need to redeclare any properties or variables.

继承你的时候不需要重新声明任何属性或变量。

Perhaps if you post your ClassB header file or a portion of then people can better pinpoint your problem.

也许如果你发布你的ClassB头文件或一部分人,可以更好地查明你的问题。

#5


0  

Correct, you just declare a @property outside of the typical member variable declaration curly brackets and then @synthesize the property in the .m file. I did notice that in the child class you have use self.propertyName to reference it but in the parent class you can just use the instant variable name.

更正,您只需在典型的成员变量声明大括号之外声明一个@property,然后@synthesize .m文件中的属性。我注意到在子类中你使用self.propertyName来引用它,但是在父类中你可以使用即时变量名。