Objective-C方法与可选需求方法快速冲突。

时间:2023-01-15 18:29:29

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

在Xcode更新之后,编译器开始在工作代码上抛出一个错误(两个函数都在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'

/用户/ . . / AppDelegate。swift:14:11: Objective-C方法的应用程序:didFinishLaunchingWithOptions:“通过方法”应用程序(:didFinishLaunchingWithOptions:)“与可选需求方法的冲突”应用程序(:didFinishLaunchingWithOptions:)在协议“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'

/用户/ . . / AppDelegate。swift:25:11: Objective-C method 'application:openURL:sourceApplication: ' by method 'application(:openURL:sourceApplication:annotation:)'与可选需求方法'application(:openURL:sourceApplication:annotation:)的冲突

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.

我知道我希望你们把这两个函数结合在一起。我不理解为什么这个代码在6.3中突然停止工作,尽管它在6.2中工作。

5 个解决方案

#1


76  

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:

我不确定为什么编译器会抛出错误,但是我确实看到了相同方法的默认Swift版本的差异。也许您可以将函数声明替换为普通Swift项目创建的声明:

1

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

func应用程序(应用程序:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

2

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

func应用程序(应用程序:UIApplication, openURL url url: NSURL, sourceApplication: String?,注释: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.

编辑1(9/21/2015):我已经确认这些是最新的Xcode 7的公开发布。在声明#2中,他们删除了可选的(annotation: AnyObject?)并将其设置为(annotation: AnyObject)。

#2


24  

The type of the launchOptions parameter of the didFinishLaunchingWithOptions function was changed in XCode 6.3:

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

"launchOptions: NSDictionary?" has become "launchOptions: [NSObject: AnyObject]?"

“launchOptions: NSDictionary?”已经变成了“launchOptions: [NSObject: AnyObject]?”

Just change your function header to match the following:

只需更改函数头以匹配以下内容:

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

#3


5  

You should also make sure you are using the correct type. Use String instead of NSString.

您还应该确保使用了正确的类型。使用字符串代替NSString。

#4


2  

Try overriding that method again from Xcode completions. Worked for me.

尝试从Xcode补全中重写该方法。为我工作。

#5


0  

launchOptions have been changed; try changing out "launchOptions: NSDictionary?" to "launchOptions: [NSObject: AnyObject]?"

launchOptions已经改变;尝试更改“launchOptions: NSDictionary?”来“launchOptions: [NSObject: AnyObject]?”

Hope this helps!

希望这可以帮助!

#1


76  

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:

我不确定为什么编译器会抛出错误,但是我确实看到了相同方法的默认Swift版本的差异。也许您可以将函数声明替换为普通Swift项目创建的声明:

1

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

func应用程序(应用程序:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

2

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

func应用程序(应用程序:UIApplication, openURL url url: NSURL, sourceApplication: String?,注释: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.

编辑1(9/21/2015):我已经确认这些是最新的Xcode 7的公开发布。在声明#2中,他们删除了可选的(annotation: AnyObject?)并将其设置为(annotation: AnyObject)。

#2


24  

The type of the launchOptions parameter of the didFinishLaunchingWithOptions function was changed in XCode 6.3:

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

"launchOptions: NSDictionary?" has become "launchOptions: [NSObject: AnyObject]?"

“launchOptions: NSDictionary?”已经变成了“launchOptions: [NSObject: AnyObject]?”

Just change your function header to match the following:

只需更改函数头以匹配以下内容:

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

#3


5  

You should also make sure you are using the correct type. Use String instead of NSString.

您还应该确保使用了正确的类型。使用字符串代替NSString。

#4


2  

Try overriding that method again from Xcode completions. Worked for me.

尝试从Xcode补全中重写该方法。为我工作。

#5


0  

launchOptions have been changed; try changing out "launchOptions: NSDictionary?" to "launchOptions: [NSObject: AnyObject]?"

launchOptions已经改变;尝试更改“launchOptions: NSDictionary?”来“launchOptions: [NSObject: AnyObject]?”

Hope this helps!

希望这可以帮助!