Objective-C 方法与可选要求方法冲突 Swift

Objective-C method conflicts with optional requirement method Swift

Xcode更新后,编译器开始在工作代码上抛出错误(两个函数都在AppDelegate.swift)。

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

有错误:

/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'

第二个

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

有错误

/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'

我知道我很可能希望您以某种方式将这两个功能合二为一。我不明白为什么这段代码在 6.3 中突然停止工作,尽管它在 6.2 中工作。

我不确定编译器抛出错误的确切原因,但我确实看到这些相同方法的默认 Swift 版本有所不同。也许您可以将函数声明替换为使用普通 Swift 项目创建的函数声明:

1

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

2

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

我建议用上面的方法替换你的方法声明,看看它现在是否可以编译。


编辑 1 (9/21/2015):我已经确认这些现在是 Xcode 7 的 public 版本的最新版本。他们在声明 #2 中删除了可选的 (annotation: AnyObject?) 并使其成为 (annotation: AnyObject)。

didFinishLaunchingWithOptions 函数的 launchOptions 参数的类型已在 XCode 6.3 中更改:

"launchOptions: NSDictionary?" 变成了 "launchOptions: [NSObject: AnyObject]?"

只需更改您的函数 header 以匹配以下内容:

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

您还应确保使用的类型正确。使用 String 而不是 NSString.

尝试从 Xcode 次完成中再次覆盖该方法。为我工作。

启动选项已更改;尝试将 "launchOptions: NSDictionary?" 更改为 "launchOptions: [NSObject: AnyObject]?"

希望对您有所帮助!