Iphone NSLog正在修复丢失的值/崩溃?

Iphone NSLog正在修复丢失的值/崩溃?,iphone,objective-c,ios,Iphone,Objective C,Ios,我的应用程序使用Social.framework和ACAccount从Facebook导入喜欢的内容。我将Facebook帐户标识符保存到NSUserDefaults,这样应用程序就可以在后续发布时自动导入新的喜好。第一个导入很好。问题在于在后续启动时重新实例化ACAccount 因此,我有一个facebookAccountgetter,看起来像这样: - (ACAccount *)facebookAccount { if(!_facebookAccount) { ACA

我的应用程序使用
Social.framework
ACAccount
从Facebook导入喜欢的内容。我将Facebook帐户标识符保存到
NSUserDefaults
,这样应用程序就可以在后续发布时自动导入新的喜好。第一个导入很好。问题在于在后续启动时重新实例化
ACAccount

因此,我有一个
facebookAccount
getter,看起来像这样:

- (ACAccount *)facebookAccount {
    if(!_facebookAccount) {
        ACAccountStore *accountStore = [[ACAccountStore alloc] init];
        NSString *accountIdentifier = [[NSUserDefaults standardUserDefaults] valueForKey:SWFacebookAccountIdentifierKey];

        if(accountIdentifier)
            _facebookAccount = [accountStore accountWithIdentifier:accountIdentifier];

    }
    return _facebookAccount;
}
这将返回一个不完整的
ACAccount
对象:

type:(null)
identifier: B6E94A67-AF94-408F-A618-6CD4D78564DC
accountDescription: Facebook
username: samvermette@gmail.com
objectID: x-coredata://589C098E-F829-4284-841B-EE4A0003FF21/Account/p2
enabledDataclasses: {(
    )}
enableAndSyncableDataclasses: {(
    )}
properties: {
    fullname = "Sam Vermette";
    uid = 716308665;
}
parentAccount: (null)
owningBundleID:(null) 
这将从最终使用此对象的方法中记录。如您所见,
类型
值为
null
,这使得我的应用程序抛出以下异常:

NSInvalidArgumentException',原因:'此请求的帐户类型无效


现在,最奇怪的事情是,如果我在getter中的
return
之前添加
NSLog(\u facebookAccount)
,则
type
键不为null,我的应用程序也不会崩溃。我意识到NSLog修复这一问题可能暗示了我的代码有问题,但我不知道到底是什么。有什么想法吗?

问题在于ARC自动释放帐户存储,该帐户存储拥有
ACAccount
对象

我通过将我的帐户存储分配给一个强属性来修复此问题

@property (nonatomic, strong) ACAccountStore *accountStore;

您必须保留_facebookAccount,如果您在返回后写入NSLog,这将显示返回的垃圾值。@fibnochi他使用的是ARC,因此他不能使用retain。实际上,Sam可以将其存储在某个ivar或全局变量中,然后尝试打印它。facebookAccount被设置为强属性,因此它会被保留。问题是我还需要保留accountStore,这很有帮助。上升。谢谢