简体   繁体   中英

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
    }

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 , ie non-optional.

You need to unwrap the input sourceApplication value, either with if-let , or default it with ?? ie 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.

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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM