27

安装 Xcode 8 beta 6 后,我收到一条警告:

实例方法 'application(_:didFinishLaunchingWithOptions:)' 几乎匹配协议 'UIApplicationDelegate' 的可选要求 'application(_:didFinishLaunchingWithOptions:)'

在我的应用程序代表中。

有 2 个建议的修复方法可以使警告静音:

  1. 将方法标记为私有
  2. 在方法中添加@nonobjc

执行任一操作都会使警告静音。但是为什么需要这样做呢?

4

2 回答 2

53

iOS 12 SDK 更新

在 iOS 12 SDK(Xcode 10 附带)中,UIApplicationLaunchOptionsKey 现在已重命名为嵌套类型UIApplication.LaunchOptionsKey,因此您需要:

func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
    // ...
}

iOS 10 和 11 SDK(Xcode 8 和 9)

这个警告是因为委托方法的didFinishLaunchingWithOptions:参数application(_:didFinishLaunchingWithOptions:)现在被桥接到 Swift 作为 a [UIApplicationLaunchOptionsKey: Any]?,而不是[NSObject : AnyObject]?.

Therefore you'll need to update your implementation to reflect this change:

func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
    // ...
}

Note that neither of Xcode's suggested fixes will actually fix the problem, they'll only conceal your implementation of application(_:didFinishLaunchingWithOptions:) from Objective-C – meaning that it'll never actually get called.

于 2016-08-16T11:05:47.757 回答
3

传递给函数的第一个参数不再具有外部名称。这实际上只是一个小细节,因为您不直接调用此方法,这是使编译器满意的快速修复。您可以手动将第一个参数名称编辑为 _,或者让 Xcode 为您处理。

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

或新语法

func application(_ application:UIApplication, 
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool // or remove = nil and try

您可以在此处从Apple获取最新文档和示例链接

于 2016-08-16T10:31:38.207 回答