【问题标题】:Error in Spotify's iOS SDK tutorialSpotify 的 iOS SDK 教程中的错误
【发布时间】:2015-11-25 19:04:27
【问题描述】:

我目前正在关注 Spotify 关于 iOS SDK 的教程。我还将 Objective-C 代码转换为 Swift。

Spotify 想让我运行这段代码:

-(BOOL)application:(UIApplication *)application
           openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
        annotation:(id)annotation {

    // Ask SPTAuth if the URL given is a Spotify authentication callback
    if ([[SPTAuth defaultInstance] canHandleURL:url]) {
        [[SPTAuth defaultInstance] handleAuthCallbackWithTriggeredAuthURL:url callback:^(NSError *error, SPTSession *session) {

             if (error != nil) {
                 NSLog(@"*** Auth error: %@", error);
                 return;
             }

             // Call the -playUsingSession: method to play a track
             [self playUsingSession:session];
        }];
        return YES;
    }

    return NO;
}

我已将其转换为 Swift:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        if SPTAuth.defaultInstance().canHandleURL(url) {
            SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSErrorPointer, session: SPTSession) -> Void in
                if error != nil {
                    NSLog("*** Auth error: %@", error)
                    return
                }
                playUsingSession(session)
            })
            return true
        }
        return false
    }

然而,swift 代码包含 2 个错误:

1)

为什么我在遵循 Spotify 的教程时会收到错误消息?是否与将 Objective-C 转换为 Swift 有关?我该如何解决这个问题?

2)

【问题讨论】:

  • 我建议您在iOS SDK tutorial page 上发表评论(在底部,单击“显示 cmets”),因为这是报告教程问题的正确位置。

标签: ios spotify cocoalibspotify-2.0


【解决方案1】:

当您处理callback 时,您将错误转换为NSErrorPointer 而不是NSError

SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSErrorPointer, session: SPTSession) -> Void in
                if error != nil {
                    NSLog("*** Auth error: %@", error)
                    return
                }
                playUsingSession(session)
 }) 

应该是

SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSError, session: SPTSession) -> Void in
                if error != nil {
                    NSLog("*** Auth error: %@", error)
                    return
                }
                playUsingSession(session)
 })

【讨论】:

    猜你喜欢
    • 2015-03-25
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    相关资源
    最近更新 更多