質問

I try to write a custom setter method but I fail with the error message "Type of property 'pValue' does not match type of accessor 'setPValue:'". The array is passed successfully but I don't understand the the error message.

my .h-file:

@property (nonatomic, assign) double pValue;

- (double)getInverseCDFValue:(double)p;
- (void)setPValue:(NSArray*)aArray;

my implementation:

@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;
}

It works with passing single values but not with arrays. Is this not allowed in setter methods?

Thanks

役に立ちましたか?

解決

When you synthesize your property, Xcode is going to generate setter and getter for your property unless your property is read-only. In this case, it's going to create only getter method for you. So, by naming convention, let's say you have a property name toto which type is double, your setter will be

-(void)setToto:(double d).

But what you can do is create another method set your property. But, you need to change your method name to something else. For example,

-(void)setTotoWithNSArray:(NSArray *).

他のヒント

You've defined a property pValue, which is of type double. Objective-C now expects that an accessor and a getter exist, with the names:

- (double)pValue; // Getter
- (void)setPValue:(double)value; // Setter

Either you let Objective-C autogenerate them for you, or you need to provide them explicitly.

Now you are providing a method with the same name, but a different type (NSArray). The type is different, and that's what the error message is telling you: the compiler thinks that the method is setter of your property, but you've given it the wrong type. It tells you because since you violate the expectations, the property cannot be used correctly in some contexts like Key-Value Coding. In almost all cases, what you've done is considered to be a bug.

You could make the pValue property read-only, I think that would make the warning disappear but it still would be bad because the type difference is confusing. Instead, rename the setter with the array argument to something else:

- (void)setPValueFromArray:(NSArray *)anArray;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top