Reputation: 6547
I am currently following Spotify's tutorial on the iOS SDK. I am also converting the Objective-C code t Swift.
Spotify wants me to run this code:
-(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;
}
I have converted it to 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
}
The swift code, however, contains 2 errors:
Why am I getting an error when following Spotify's tutorial? Is it to do with converting Objective-C to Swift? How would I fix this?
Upvotes: 0
Views: 304
Reputation: 36
When you handle the callback
, you are casting the error as a NSErrorPointer
rather than NSError
.
SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSErrorPointer, session: SPTSession) -> Void in
if error != nil {
NSLog("*** Auth error: %@", error)
return
}
playUsingSession(session)
})
should be
SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSError, session: SPTSession) -> Void in
if error != nil {
NSLog("*** Auth error: %@", error)
return
}
playUsingSession(session)
})
Upvotes: 2