Spotify Renew Session致命错误:在展开Optional值时意外发现nil

时间:2015-12-14 01:26:06

标签: swift spotify access-token optional

尝试从Spotify续订会话,以便我可以检索新访问权限并刷新令牌。从第一个打印行开始,我收到致命错误。

@IBAction func renewSession(sender: AnyObject) {

    let auth = SPTAuth.defaultInstance()
    auth.tokenRefreshURL = NSURL(string: tokenRefresh)
    auth.tokenSwapURL = NSURL(string: tokenSwap)
    auth.session = self.spotifySession
    auth.sessionUserDefaultsKey = sessionKey

    if (auth.session != nil) {

        SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session, callback: {(error, session) -> Void in

            if error != nil {

                print("The renewed Spotify session is", session)
                print("The renewed canonical user name in the session is", session.canonicalUsername)
                print("The renewed access Spotify token in session is - %@", auth.session.accessToken)
                print("The renewed encrypted refresh Spotify token in session is - %@", auth.session.encryptedRefreshToken)
                print("The renewed expiration date of the Spotify access token is - %@", auth.session.expirationDate)

            } else {

                print ("The problem with the renewal session is", error)

            }

        })

    }

1 个答案:

答案 0 :(得分:0)

您应该检查if session != nil,而不是if error != nil。有一些error,但您的代码正在将控制权移至错误的流程。