0

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:

1) enter image description here

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?

2) enter image description here

iProgram
  • 6,057
  • 9
  • 39
  • 80
  • I recommend you to post a comment on the [iOS SDK tutorial page](https://developer.spotify.com/technologies/spotify-ios-sdk/tutorial/) (at the bottom, click on "Show comments") since that's the proper place to report issues with the tutorial. – José M. Pérez Dec 09 '15 at 07:09

1 Answers1

2

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)
 })
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
SwiftStuff
  • 36
  • 2