Nurdin
Nurdin

Reputation: 23893

Cannot invoke '*' with an argument list of type '(NSURL, *: NSString?)'

I just updated my Xcode to 6.3.1 The problem is my Facebook integration suddenly generate weird errors. Before this (Swift 1.1 and Xcode 6.2) no issue at all.

Error message 1

/Users/MNurdin/Documents/iOS/xxxxx/AppDelegate.swift:33:41: Cannot invoke 'handleOpenURL' with an argument list of type '(NSURL, sourceApplication: NSString?)'

Error message 2

/Users/MNurdin/Documents/iOS/xxxxx/AppDelegate.swift:32:10: Objective-C method 'application:openURL:sourceApplication:annotation:' provided by method 'application(:openURL:sourceApplication:annotation:)' conflicts with optional requirement method 'application(:openURL:sourceApplication:annotation:)' in protocol 'UIApplicationDelegate'

My code

func application(application: UIApplication, openURL url: NSURL, sourceApplication: NSString?, annotation: AnyObject) -> Bool { //error message 1 here
        var wasHandled:Bool = FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication) //error message 2 here
        return wasHandled
    }

Upvotes: 1

Views: 1474

Answers (2)

Nurdin
Nurdin

Reputation: 23893

Thank you @airspeed . I already fully understand about the optional. So what I do right now is like this

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { //error message 1 here
        var wasHandled:Bool = FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication!) //error message 2 here
        return wasHandled
    } 

It works perfectly!!

Upvotes: 0

Airspeed Velocity
Airspeed Velocity

Reputation: 40965

The problem is that in your function declaration, sourceApplication is an optional:

func application(application: UIApplication, 
        openURL url: NSURL, 
        sourceApplication: NSString?,     // note, NSString? so optional
        annotation: AnyObject) -> Bool

but in the call to FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication), sourceApplication is NSString, i.e. non-optional.

You need to unwrap the input sourceApplication value, either with if-let, or default it with ?? i.e. FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication ?? "defaultAppName"), or even better if you control the declaration of the application func, have it not be optional in the first place.

Upvotes: 1

Related Questions