-1

I'm new to Objective C and I'm having a problem reading NSMutableDisctionary from another class. NSMutableDictionary itself is working fine in ViewController class, but when I try to read it in TestResults class i get empty dictionary. Where did I go wrong? Thanks!

ViewController.h

@property NSMutableDictionary *storedAnswers;

ViewController.m

@implementation ViewController
@synthesize questionScrollView;
- (id)init
{
    self = [super init];
    if (self) {
        [_storedAnswers setObject:@"aaa" forKey:@"bbb"];
    }
    return self;
}

TestResults.h

#import "ViewController.h"

ViewController *answers = [[ViewController alloc] init];
NSLog(@"%@",answers.storedAnswers);
Domas
  • 1
  • 2

4 Answers4

2

You're accessing the dictionary just fine. You're not accessing the correct ViewController instance though. You need to get a reference to the view controller--not just instantiate another instance of the view controller.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

What makes you think that _storedAnswers would be anything other than nil?

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

_storedAnswers is never initialized (that we can see here). Add:

_storedAnswers = [NSMutableDictionary new];

before you start using it.

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • this could do the job, as it passes data to another class but then i have to add some more data to this dictionary and it wont happen... – Domas Mar 14 '14 at 22:18
0

ViewController.h

@property NSMutableDictionary *storedAnswers;

ViewController.m

@implementation ViewController
@synthesize questionScrollView;
- (id)init
{
    self = [super init];
    if (self) {
        _storedAnswers = [NSMutableDictionary alloc] init];
        [_storedAnswers setObject:@"aaa" forKey:@"bbb"];
    }
    return self;
}

- (void) dealloc
{
    _storedAnswers = nil; // free memory if ARC is enable
}
Khalil
  • 238
  • 2
  • 15
  • this passes the data. but what about if i want to add more data to this dictionary outside this method in ViewController.m ? – Domas Mar 14 '14 at 22:45
  • ViewController *answer = [[ViewController alloc] init]; [answer.storedAnswer setObject:@"secondObject" forKey:@"secondKey"]; – Khalil Mar 14 '14 at 22:51
  • so i would you use this in a second class. what about adding some more data in a class where that dictionary originally is? – Domas Mar 14 '14 at 23:03
  • Domas, can you more explain? – Khalil Mar 14 '14 at 23:10
  • Right.. Your given answer technically works. It's a method that creates NSMutableDictionary and adds some data in it. That's fine. But then later in a same class I have more methods where I want to put more data in to the same dictionary. And in the end, i want to read all that data in another class.. hope you understood me – Domas Mar 14 '14 at 23:15
  • if you want read data from dictionary use this: [_answerStored ObjectForKey:@"secondKey"]; this return value of key; and if you want get all keys or value you can use [_storedAnswer allKeys] or [_storedAnswer allValues]; // sorry for my bad english – Khalil Mar 14 '14 at 23:24
  • ok, but i want to insert more data, not read – Domas Mar 14 '14 at 23:26
  • [_storedAnswers setObject:@"bbb" forKey:@"Key1"]; [_storedAnswers setObject:@"bbb" forKey:@"Key2"]; [_storedAnswers setObject:@"bbb" forKey:@"Key3"]; and more ? what is this? – Khalil Mar 14 '14 at 23:27
  • but if i use this line of code in different methods that doesn't work and this is a problem.. – Domas Mar 14 '14 at 23:29
  • no, the data is just not added.. i guess i need to read some tutorials.. – Domas Mar 14 '14 at 23:34