0

我对objective-c完全陌生,我目前正在学习过程中,我有一个父类A,它有一个属性a,我试图访问子类B中的属性,当我访问属性并分配一个值时这个

[self a:3];

它确实抱怨 No visible @interface for B 声明选择器 a

但是如果我像 int something = [self a] 那样访问它来读取它;然后它不会抱怨。

我了解访问属性的推荐方法是使用 . 在对象和属性之间,但从技术上讲,它应该与消息样式调用一起使用。但事实并非如此,所以请就此向我提出建议。

我的代码是这样的

// Test class A
@interface A : NSObject

@property int a;

-(void) initMe; 

@end

@implementation A

@synthesize a;

-(void) initMe
{
 NSLog(@"I am in A");
}
@end

//-------------------------

@interface B : A

-(void) initEx; 

@end

@implementation B

-(void) initEx
{
    // This line gives a problem as I mentioned above
    [self a:3];
    NSLog(@"In child class B");
}

@end

///-----------------------
4

1 回答 1

3

[self a:3];是错误的语法。如果要调用setter方法,应该是:

[self setA:3];
于 2012-06-11T12:08:54.277 回答