NSDictionary 变量未传递给其他方法

发布于 2024-10-14 05:08:56 字数 734 浏览 5 评论 0原文

我有一个方法,它从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

£烟消云散 2024-10-21 05:08:56

如果您说 cardDictionary 是全局变量,而 self.cardDictionary - 可能会访问实例变量。

并且在

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

}

你正在访问全球之一。删除它并使用实例变量。

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.

2024-10-21 05:08:56

您确定您的第二个调用 (parseDictionaryForCardPage) 使用与第一个调用 (findIndexForCardPage) 相同的 RequestModel 实例吗?

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

葬花如无物 2024-10-21 05:08:56

您在 RequestModal 类中的属性应声明为保留:

@property (nonatomic,retain) NSDictionary* cardDictionary;

在类 RequestModal 的 @implementation 部分中,您应该合成属性:

@synthesize cardDictionary;

并且此方法是正确的:

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

在此方法中,您应该添加 self.property 。到财产名称:

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

}

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文