I’m declaring my dictionary like this:
@property (weak, nonatomic) NSMutableDictionary *testVariableValues;
@synthesize testVariableValues = _testVariableValues;
Then, I’m populating the dictionary like this:
- (IBAction)testPressed:(UIButton *)sender {
self.testVariableValues = [NSMutableDictionary dictionary];
if ([sender.currentTitle isEqualToString:@"Test 1"])
{
[self.testVariableValues setObject:[NSNumber numberWithDouble:5.2] forKey:@"x"];
[self.testVariableValues setObject:[NSNumber numberWithInt:-1] forKey:@"y"];
[self.testVariableValues setObject:[NSNumber numberWithInt:1] forKey:@"a"];
} else if ([sender.currentTitle isEqualToString:@"Test 2"]) {
// Continues like this
Each time I populate the dictionary I’m printing the contents of the dictionary to the command line so I know that that part is working, the problems seem to arise if I’m trying to access the dictionary from another method, like this:
if ([self.display.text isEqualToString:@"x"]) {
NSLog(@"%f", [[self.testVariableValues objectForKey:@"x"] doubleValue]);
[self.brain pushOperand:[[self.testVariableValues objectForKey:@"x"] doubleValue]];
The NSLog in this block of code returns null, which makes me think that I can’t access the dictionary from outside the testPressed method. Is someone able to shed some light on this? Is my implementation of the dictionary all wrong?
Thanks!
Your dictionary should be a strong property, since if you make it weak, the ditionary will be freed by the time
testPressedmethod ends executionyou should change
to