Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone AppDelegate实例变量获取僵尸_Iphone_Objective C_Ios_Xcode - Fatal编程技术网

Iphone AppDelegate实例变量获取僵尸

Iphone AppDelegate实例变量获取僵尸,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,在AppDelegate中,我有一个用户变量,以便在整个应用程序中都可以访问。授权代码如下所示: @interface MyAppDelegate : NSObject <UIApplicationDelegate, ...> { @private User *_appUser; ... } - (void)setUser:(User *)value; - (User *)appUser; - (void)setUser:(User *)value { @synchron

在AppDelegate中,我有一个用户变量,以便在整个应用程序中都可以访问。授权代码如下所示:

@interface MyAppDelegate : NSObject <UIApplicationDelegate, ...> {

@private User *_appUser;
...
}

- (void)setUser:(User *)value;
- (User *)appUser;

- (void)setUser:(User *)value
{
   @synchronized(self)
   {
    if(_appUser != value)
    {
        [_appUser release];
        _appUser = [value retain];
        [NSUserDefaultsManager SaveUser:_appUser];
    }
}
第一次,当用户注册时,我通过ViewController通过

[((MyAppDelegate *)[[UIApplication sharedApplication] delegate]) setUser:user];
User *user = [((MyAppDelegate *)[[UIApplication sharedApplication] delegate]) appUser];
问题是,当我需要通过

[((MyAppDelegate *)[[UIApplication sharedApplication] delegate]) setUser:user];
User *user = [((MyAppDelegate *)[[UIApplication sharedApplication] delegate]) appUser];
在AppDelegate的appUser方法中,_AppUserIVAR是僵尸。 有解决办法吗?
提前谢谢。

你的getter方法是罪魁祸首吗?返回的_appUser没有通常的保留/自动释放模式。您能否通过使用getter方法解决此问题:

- (User *)appUser
{
   if(_appUser == nil)
    return [NSUserDefaultsManager GetUser];
else
    return [[_appUser retain] autorelease];
}
或者


这似乎也无济于事_在这种方法中,appUser仍然是僵尸。我还在application didFinishLaunchingWithOptions中添加了_appUser=[[[User alloc]init]retain],但没有任何效果。您是否直接通过应用程序委托中的任何其他位置访问_AppUserIVAR,而不是通过访问器?另外,我不确定[NSUserDefaultsManager GetUser]方法返回的对象是什么类型(以及保留计数是多少),GetUser方法使用NSKeyedUnarchiveObjectWithData,因此我假设返回一个自动删除的对象。另外,在setter方法中,“value”是一个自动释放的对象。