0
votes

I have a method that takes an NSDictionary from a ViewController and assigns it to a global NSDictionary (cardDictionary) variable in my RequestModel class. cardDictionary is declared as a property (nonatomic, retain) and it is synthesized. That works ok (as verified by NSLog). Here it is:

-(void)findIndexForCardPage:(NSDictionary*)dictionary  {
//parse here and send to a global dictionary
self.cardDictionary = dictionary;
NSLog(@"%@",cardDictionary);
}

Now when I try to access that variable in another method, still in the RequestModel class, it is null:

-(NSDictionary*)parseDictionaryForCardPage  {
//parse dictionary here for data needed on card page
NSLog(@"%@",cardDictionary);
return cardDictionary;

}

This method is called from a viewcontroller class, and cardDictionary is null. How can I make cardDictionary retain its data from the previous method? I tried taking out the self from self.cartDictionary...no luck. Anything else I should know?

3

3 Answers

1
votes

If you are saying that cardDictionary is the global variable, and self.cardDictionary - probably accessing the instance variable.

and in

-(NSDictionary*)parseDictionaryForCardPage  {
//parse dictionary here for data needed on card page
NSLog(@"%@",cardDictionary);
return cardDictionary;

}

you are accessing the global one. Delete it and use the instance variable.

1
votes

Are you sure your second call (parseDictionaryForCardPage) is using the same instance of RequestModel that the first call (findIndexForCardPage) is?

0
votes

You property in RequestModal class should be declared as retained:

@property (nonatomic,retain) NSDictionary* cardDictionary;

In the @implementation section of class RequestModal you should synthesize property:

@synthesize cardDictionary;

and this method is correct:

-(void)findIndexForCardPage:(NSDictionary*)dictionary  {
//parse here and send to a global dictionary
self.cardDictionary = dictionary;
NSLog(@"%@",cardDictionary);
}

In this method you should add self. to the name of property:

-(NSDictionary*)parseDictionaryForCardPage  {
//parse dictionary here for data needed on card page
NSLog(@"%@",self.cardDictionary);
return cardDictionary;

}