Spotify的iOS SDK教程出错

时间:2015-11-25 19:04:28

标签: ios spotify cocoalibspotify-2.0

我目前正在关注iOS SDK上的Spotify教程。我也在转换Objective-C代码t 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)enter image description here

为什么我在遵循Spotify教程时遇到错误?是否将Objective-C转换为Swift?我该如何解决这个问题?

2)enter image description here

1 个答案:

答案 0 :(得分:2)

当您处理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)
 })