NSDictionary 变量未传递给其他方法
我有一个方法,它从 ViewController 获取 NSDictionary 并将其分配给我的 RequestModel 类中的全局 NSDictionary (cardDictionary) 变量。 cardDictionary 被声明为属性(非原子、保留)并被合成。工作正常(经 NSLog 验证)。现在,
-(void)findIndexForCardPage:(NSDictionary*)dictionary {
//parse here and send to a global dictionary
self.cardDictionary = dictionary;
NSLog(@"%@",cardDictionary);
}
当我尝试在另一个方法(仍在 RequestModel 类中)访问该变量时,它为 null:
-(NSDictionary*)parseDictionaryForCardPage {
//parse dictionary here for data needed on card page
NSLog(@"%@",cardDictionary);
return cardDictionary;
}
该方法是从 viewcontroller 类调用的,并且 cardDictionary 为 null。如何使 cardDictionary 保留先前方法中的数据?我尝试从 self.cartDictionary 中取出 self...没有运气。还有什么我应该知道的吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您说 cardDictionary 是全局变量,而 self.cardDictionary - 可能会访问实例变量。
并且在
你正在访问全球之一。删除它并使用实例变量。
If you are saying that cardDictionary is the global variable, and self.cardDictionary - probably accessing the instance variable.
and in
you are accessing the global one. Delete it and use the instance variable.
您确定您的第二个调用 (parseDictionaryForCardPage) 使用与第一个调用 (findIndexForCardPage) 相同的 RequestModel 实例吗?
Are you sure your second call (parseDictionaryForCardPage) is using the same instance of RequestModel that the first call (findIndexForCardPage) is?
您在 RequestModal 类中的属性应声明为保留:
在类 RequestModal 的 @implementation 部分中,您应该合成属性:
并且此方法是正确的:
在此方法中,您应该添加 self.property 。到财产名称:
You property in RequestModal class should be declared as retained:
In the @implementation section of class RequestModal you should synthesize property:
and this method is correct:
In this method you should add self. to the name of property: