Reputation: 1006
I'm trying to declare a custom getter of a NSDictionary but I can't get it to work.
So far I have the @property declared and the synthesize syntax.
@property (nonatomic, strong) NSDictionary *variableDictionary;
@synthesize variableDictionary = _variableDictionary;
I want to declare a setter for the dictionary that gives it some standard values.
-(void)setVariableDictionary:(NSDictionary *)variableDictionary {
NSNumber *x = [NSNumber numberWithDouble:20];
NSNumber *a = [NSNumber numberWithDouble:10];
NSNumber *b = [NSNumber numberWithDouble:5];
NSDictionary *_variableDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:a, @"a", b, @"b", x, @"x", nil];
}
When I use the above method I get a warning unused variable, and if I remove the underscore from the NSDictionary variable definition, I get an error saying 'redefinition of variable dictionary'.
I'm not sure of the correct way to do this.
Upvotes: 0
Views: 1956
Reputation: 7049
First off, the setter implementation doesn't really make any sense considering it will never change the value (it's only function is to initialize). Nonetheless (assuming this would change), if you want to test the value in your setter, you could make it look something like this
-(void)setVariableDictionary:(NSDictionary *)variableDictionary {
NSNumber *x = [NSNumber numberWithDouble:20];
NSNumber *a = [NSNumber numberWithDouble:10];
NSNumber *b = [NSNumber numberWithDouble:5];
_variableDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:a, @"a", b, @"b", x, @"x", nil];
NSLog(@"%@", _variableDictionary);
}
This will tell you whether or not the method is even being called. If it's not then you won't get the NSLog message. This works on my end, if it's not on yours then there is something else going on.
Upvotes: 1
Reputation: 19469
Try doing it this way, set your property as mentioned below:
@property (nonatomic, strong,setter = setTheVariableDictionary:) NSDictionary *variableDictionary;
Remove
-(void)setVariableDictionary:(NSDictionary *)variableDictionary
and write is as
-(void)setVariableDictionary:(NSDictionary *)thevariableDictionary
Hope this helps you.
Upvotes: 0
Reputation: 15722
In your custom setVarableDictionary:
setter method, when you write:
NSDictionary *_variableDictionary = _variableDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:a, @"a", b, @"b", x, @"x", nil];
this declares and sets value for a new local variable within the scope of your function only. The warning message you get is because that variable is not used before it goes out of scope.
Instead of creating a local variable, you should just set the value of the ivar that underlies your property, like this:
_variableDictionary = [NSDictionary dictionaryWithObjectsAndKeys:a, @"a", b, @"b", x, @"x", nil];
Upvotes: 1
Reputation: 8106
If I understood your question. This might help
-(void)setVariableDictionary:(NSDictionary *)newVariableDictionary
{
if (variableDictionary != newVariableDictionary) {
variableDictionary = newVariableDictionary;
}
}
in your code.
-(void)setVariableDictionary:(NSDictionary *)variableDictionary {
its normal that it will give you an error when you redeclare your parameter. (NSDictionary *)variableDictionary
Upvotes: 0
Reputation: 2226
_variableDictionary is already declared.
Replace
NSDictionary *_variableDictionary = [[NSDictionary alloc]
initWithObjectsAndKeys:a, @"a", b, @"b", x, @"x", nil];
with
_variableDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:a, @"a", b, @"b", x, @"x", nil];
Upvotes: 1