我尝试编写自定义 setter 方法,但失败并显示错误消息 “属性‘pValue’的类型与访问器‘setPValue:’的类型不匹配”
。数组已成功传递,但我不明白错误消息。
我的 .h 文件:
@property (nonatomic, assign) double pValue;
- (double)getInverseCDFValue:(double)p;
- (void)setPValue:(NSArray*)aArray;
我的实现:
@synthesize pValue;
- (void)setPValue:(NSArray *)aArray {
double p = [[aArray objectAtIndex:22]doubleValue];
NSLog(@"p is: %f", p); //the value is written successfully
[self getInverseCDFValue:p]; //just calling another method for calculations
pValue = iCFDValue;
}
它适用于传递单个值,但不适用于数组。这在 setter 方法中是不允许的吗?
谢谢
最佳答案
当您合成您的属性时,Xcode 将为您的属性生成 setter 和 getter,除非您的属性是只读。在这种情况下,它将只为您创建 getter 方法。因此,根据命名约定,假设您有一个属性名称 toto 类型是 double,您的 setter 将是
-(void)setToto:(double d)
.
但是您可以做的是创建另一种方法来设置您的属性。但是,您需要将方法名称更改为其他名称。例如,
-(void)setTotoWithNSArray:(NSArray *)
.
关于iOS 可能在 setter 方法中传递 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18773066/