0

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

JFS
  • 2,992
  • 3
  • 37
  • 48
  • 1
    I think the property's setter is expected to be - (void)setPValue:(double)pValue. Give another name to your custom setter. – Calin Chitu Sep 12 '13 at 19:56

2 Answers2

1

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 *).

limon
  • 3,222
  • 5
  • 35
  • 52
  • Hello mstfbsnli (what a name) thank you for your help. It is working now. I can take care about my remaining hairs now. – JFS Sep 12 '13 at 20:06
1

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;
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • Hallo DarkDust, excellent answer and it works now. Even your answer is really good don't be mad when I accept the answer from mstfbsnli. He answered first. – JFS Sep 12 '13 at 20:05
  • 1
    @JFS: You don't need to justify your choice of the accepted answer (as long as it's correct, and mstfbsnli's answer _is_ correct) ;-) – DarkDust Sep 12 '13 at 20:08