I can’t figure out why my getter and setter code is not working. In some example code I was going over:
- (NSArray *)sushiTypes {
return _sushiRolls;
}
- (void)setSushiTypes:(NSArray *)sushiRolls {
[sushiRolls retain];
[_sushiRolls release];
_sushiRolls = sushiRolls;
}
Then in:
- (void)viewDidLoad {
[super viewDidLoad];
self.sushiTypes = [[NSArray alloc]initWithObjects:@"...]autorelease];
}
The whole time, this worked, but a property for sushiTypes was never declared. I (sort of) get how this works, since it works the same as a setter/getter regardless of whether it was synthesized or not.
But here’s my code, and I get a compiler error asking for a property. Did I miss something?
#import <Foundation/Foundation.h>
@interface Temp0 : NSObject {
NSNumber *x1;
}
-(NSNumber *)x1;
-(void)setx1:(NSNumber *)x;
@end
//
#import "Temp0.h"
@implementation Temp0
-(NSNumber *)x1 {
return x1;
}
-(void)setx1:(NSNumber *)x {
[x retain];
[x1 release];
x1 = x;
}
-(id)init
{
self.x1 = [[NSNumber alloc]initWithInt:1]; // Error on this line:
// Setter method is needed to assign to object using property assignment syntax
[super init];
}
@end
It’s standard to capitalize the first letter of the property in the setter method name. As you correctly have in:
So
setX1:is the expected method signature.