【问题标题】:NSLog fixing missing value/crash?NSLog 修复缺失值/崩溃?
【发布时间】:2013-01-26 06:06:10
【问题描述】:

我的应用使用 Social.frameworkACAccount 从 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) 

这会从最终使用此对象的方法中记录下来。如您所见,type 的值为null,这使我的应用程序抛出以下异常:

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

现在,最奇怪的是,如果我在我的 getter 中的 return 之前添加 NSLog(_facebookAccount)type 键不为空,我的应用程序不会崩溃。我意识到修复此问题的 NSLog 可能暗示我的代码有问题,但我无法弄清楚究竟是什么。有什么想法吗?

【问题讨论】:

  • 你必须保留 _facebookAccount,如果你在返回后写 NSLog,这将显示垃圾值,因为它已经返回。
  • @fibnochi 他正在使用 ARC,所以他不能使用保留。确实,Sam,您可以将它存储在某个 ivar 或全局变量中,然后尝试打印它。
  • facebookAccount 被设置为强属性,所以它被保留。问题是我还需要保留 accountStore。

标签: iphone objective-c ios


【解决方案1】:

问题最终是 ARC 自动释放拥有 ACAccount 对象的帐户存储。

我通过将我的帐户存储分配给强大的属性来解决此问题

@property (nonatomic, strong) ACAccountStore *accountStore;

【讨论】:

  • 这有帮助。上升。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 2018-01-18
  • 2016-05-22
  • 2017-03-08
  • 2015-09-15
  • 2011-02-25
相关资源
最近更新 更多