我目前正在关注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个错误:
为什么我在遵循Spotify教程时遇到错误?是否将Objective-C转换为Swift?我该如何解决这个问题?
答案 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)
})