0

原生快速开发的新手!在https://github.com/forcedotcom/SalesforceMobileSDK-iOS/issues/2072中打开了以下问题

使用的移动 SDK 版本:5.1.0
在 Native App 或 Hybrid App 中发现问题:Native App
OS 版本:10.12.5
设备:iPhone 6

重现步骤:

  1. 强制创建
  2. 提供应用程序类型native_swift并添加其他请求的详细信息
  3. *.xcworkspace在 Xcode 中打开文件
  4. 构建项目

错误:Value id optional type '[SFUserAccount]?' not unwrapped;

    func handleSdkManagerLogout()
        {
            self.log(.debug, msg: "SFAuthenticationManager logged out.  Resetting app.")
            self.resetViewState { () -> () in
                self.initializeAppViewState()

                // Multi-user pattern:
                // - If there are two or more existing accounts after logout, let the user choose the account
                //   to switch to.
                // - If there is one existing account, automatically switch to that account.
                // - If there are no further authenticated accounts, present the login screen.
                //
                // Alternatively, you could just go straight to re-initializing your app state, if you know
                // your app does not support multiple accounts.  The logic below will work either way.

                var numberOfAccounts : Int;
                let allAccounts = SFUserAccountManager.sharedInstance().allUserAccounts()
                numberOfAccounts = (allAccounts!.count);

                if numberOfAccounts > 1 {
                    let userSwitchVc = SFDefaultUserManagementViewController(completionBlock: {
                        action in
                        self.window!.rootViewController!.dismiss(animated:true, completion: nil)
                    })
                    if let actualRootViewController = self.window!.rootViewController {
                        actualRootViewController.present(userSwitchVc!, animated: true, completion: nil)
                    }
                } else {
                    if (numberOfAccounts == 1) {
                        SFUserAccountManager.sharedInstance().currentUser = allAccounts[0]

// ERROR: Value id optional type '[SFUserAccount]?' not unwrapped;
                    }
                    SalesforceSDKManager.shared().launch()
                }
            }
        }
4

2 回答 2

1

allUserAccounts属性SFUserAccountManagernullable

- (nullable NSArray <SFUserAccount *> *) allUserAccounts;

https://github.com/forcedotcom/SalesforceMobileSDK-iOS/blob/master/libs/SalesforceSDKCore/SalesforceSDKCore/Classes/Security/SFUserAccountManager.h#L188

如果您知道它在您尝试使用它时会存在,您可以通过键入来执行强制解包allAccounts![0]。如果您需要处理它可能为 nil 的情况,您需要通过执行以下操作来检查它:

if let accounts = allAccounts
{
   currentUser = accounts[0]
}
else
{
   // does not exist
}

我不能告诉你的是它是否是 nil 实际上是你需要处理的有效案例,因为我不熟悉这个库。您需要自己进行研究或询问他们。

于 2017-06-05T21:27:00.463 回答
0

您需要解包allAccounts,因为它是一个可选数组。并且由于上面已使用强制展开来获取numberOfAccounts,因此使用它可能是安全的。

尝试这个:

SFUserAccountManager.sharedInstance().currentuser = allAccounts![0]
于 2017-06-05T21:25:37.053 回答