【问题标题】:Issues With App Delegate it frees the variableApp Delegate 的问题它释放了变量
【发布时间】:2011-12-07 08:36:08
【问题描述】:

我对 ios 非常陌生。

我正在尝试学习 appDelegate 方法来在我的应用程序中传递数据。当我为变量(appDelegate 变量)设置值时,它确实在那个时间点设置了它 但经过 2 个步骤后,它显示它超出范围。 当我这样做时检索它

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];    
    myClass *my = [appDelegate.myclass objectAtIndex:0];

它说明了我的一些 myClass 属性。(使用断点和随机数进行检查,有时是第一次,第二次是第三次)。

有谁知道我错过了什么??? 感谢您的帮助 ! :)

【问题讨论】:

  • 你能提供更多细节吗? appDelegate.myclass 是如何定义的?有时调试器无法很好地显示变量内容,当您使用 NSLog 打印变量内容时会发生什么?
  • 我使用了一个断点进行检查,它确实具有正确的值。 UserProfile *up = [[[UserProfile alloc] init]initWithUsername:un 密码:pd 地址:ad zipcode:zip email:em emailSetting:es calenderSetting:cs]; [self.userprofile addObject:up]; self.userprofile 是我的变量。 up 变量完全没问题!
  • 你能把你的问题的更多细节发给我吗...

标签: iphone ios uiapplicationdelegate


【解决方案1】:

您不能总是相信调试器能够很好地显示变量内容。如果调试器说它不在范围内,请尝试将变量内容打印到控制台:

NSLog(@"%@", [appDelegate.myclass objectAtIndex:0]);

只有当这崩溃/失败时,您才会遇到实际问题。

【讨论】:

  • nslog 的输出是 ...这意味着它正在分配一些内存,但我无法读取其中的内容!
  • 这看起来你没有问题。 NSLog 不能打印出每种类型的对象。如果您在 UserProfile 中有任何字符串成员,您可以尝试将其打印出来以确定
  • 你能指出你项目中的相关文件吗?它相当大:)
【解决方案2】:

试试这个。

 if([appDelegate.myclass objectAtIndex:0]!=nil)
 {
       //Do your Code
 }
 Else
 {
       //No Values
 }

【讨论】:

  • 我试过把它放在 if 部分!你能提出更多建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2011-05-26
  • 2019-02-07
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多