Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

ios - Objective-C method conflicts with optional requirement method Swift

After the Xcode update, the compiler began to throw an error on the working code (both functions are in the AppDelegate.swift).

func application(application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool 
{
    FBLoginView.self
    FBProfilePictureView.self
    return true
}

With error:

/Users/../AppDelegate.swift:14:11: Objective-C method 'application:didFinishLaunchingWithOptions:' provided by method 'application(:didFinishLaunchingWithOptions:)' conflicts with optional requirement method 'application(:didFinishLaunchingWithOptions:)' in protocol 'UIApplicationDelegate'

And second

func application(application: UIApplication,        
                            openURL url: NSURL,
                            sourceApplication: NSString?,
                            annotation: AnyObject) -> Bool {
    var wasHandled:Bool = FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication as! String)
    return wasHandled
}

with error

/Users/../AppDelegate.swift:25:11: 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'

I understand that most likely I should like you to stick together somehow these two functions into one. I do not understand why this code suddenly stopped working in 6.3, despite the fact that it worked in 6.2.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not sure exactly why the compiler is throwing the error, however I do see a difference in the default Swift version of those same methods. Perhaps you could replace the function declaration with those created with a normal Swift project:

1

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

2

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool

I'd recommend replacing your method declarations with the above to see if it compiles now.


EDIT 1 (9/21/2015): I've confirmed these are now up to date for Xcode 7's public release. They removed the optional (annotation: AnyObject?) and made it (annotation: AnyObject), in declaration #2.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...