Xcode 8 警告“实例方法几乎匹配可选要求”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39495773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 8 Warning "Instance method nearly matches optional requirement"
提问by codingFriend1
I converted my (macOS) project to Swift 3 in Xcode 8 and I get the following warnings with several delegate methods I implement in swift classes:
我在 Xcode 8 中将我的 (macOS) 项目转换为 Swift 3,并且我在 swift 类中实现的几个委托方法收到以下警告:
Instance method 'someMethod' nearly matches optional requirement of protocol 'protocolName'
I get this for several NSApplicationDelegate methods like applicationDidFinishLaunching
and applicationDidBecomeActive
:
我为几个 NSApplicationDelegate 方法得到了这个,比如applicationDidFinishLaunching
和applicationDidBecomeActive
:
But also for implementations of tableViewSelectionDidChange
:
但也适用于tableViewSelectionDidChange
:
I used code completion to insert the method signatures and also tried copying them from the SDK-headers to rule out typos. The warnings just don't disappear and the methods are never called.
我使用代码完成来插入方法签名,并尝试从 SDK 头文件中复制它们以排除拼写错误。警告不会消失,方法也不会被调用。
What am I missing here?
我在这里错过了什么?
采纳答案by codingFriend1
We contacted Apple Developer Technical Support (DTS) with this issue. They replied that this is a bug in Xcode 8.
我们就此问题联系了 Apple 开发人员技术支持 (DTS)。他们回答说这是Xcode 8 中的一个错误。
We submitted a bug report and hope for a quick update. (Apple Bug Report ID: 28315920).
我们提交了错误报告,希望能快速更新。(Apple 错误报告 ID:28315920)。
If you experience a similar issue, please also file a bug report(referring to ours) so the Apple engineers see its not a single case.
如果您遇到类似的问题,请同时提交错误报告(参考我们的),以便 Apple 工程师看到它不是一个单独的案例。
Update for Xcode ≥ 8.1
更新 Xcode ≥ 8.1
The problem seems fixed now, at least for the delegate methods we are using in our project.
现在问题似乎已解决,至少对于我们在项目中使用的委托方法而言。
回答by MickCrozier
After hours of searching I found this - Swift 3 ObjC Optional Protocol Method Not Called in Subclass
经过数小时的搜索,我发现了这个 - Swift 3 ObjC Optional Protocol Method Not Called in Subclass
You can workaround the bug by prefixing the objective-c declaration on the function
您可以通过在函数上添加 Objective-c 声明的前缀来解决该错误
@objc(tableViewSettingsDidChange:notification:)
func tableViewSettingsDidChange(_ notification:Notification)
回答by DCDC
One reason you might get this error has to do with method access modifiers. For example if you didn't define the function as public. So for methods on the CLLocationManagerDelegate case, changing:
您可能会收到此错误的原因之一与方法访问修饰符有关。例如,如果您没有将该函数定义为 public。因此,对于 CLLocationManagerDelegate 情况下的方法,更改:
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus)
to:
到:
public func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus)
(i.e. make the method public) gets rid of the warning and the method get called as expected. Note that autocomplete doesn't put the public access modifier on the method.
(即使方法公开)摆脱警告,并按预期调用该方法。请注意,自动完成不会将公共访问修饰符放在方法上。
回答by Rory O'Bryan
Another cause of this warning when NSError
is being bridged to Swift:
NSError
桥接到 Swift时出现此警告的另一个原因:
Given this Objective-C delegate method:
鉴于此 Objective-C 委托方法:
- (void)myService:(id<MYService>)myService didFailForSomeReason:(NSError *)error;
That automatically generates this Swift method:
这会自动生成这个 Swift 方法:
public func myService(_ myService: MYService!, didFailForSomeReason error: Error!)
The warning was shown.
显示了警告。
In my case the reason was that my class had it's own Error
type so the signature was resolving to MyClass.Error
rather than Swift.Error
. The solution was to fully type the Error parameter by changing it to Swift.Error
:
就我而言,原因是我的班级有自己的Error
类型,因此签名解析为MyClass.Error
而不是Swift.Error
. 解决方案是通过将其更改为完全键入错误参数Swift.Error
:
public func myService(_ myService: MYService!, didFailForSomeReason error: Swift.Error!)
回答by Sultan Ali
for me the problem was custom Error
class
Basically I had my own class with name Error
and compiler was considering delegate method
as a local method
对我来说,问题是自定义Error
类
基本上我有自己的类,名称Error
和编译器正在考虑delegate method
作为本地方法
I just changed name of my own class name and it worked. So, just confirm that you don't have any class name same in the function
我只是更改了我自己的班级名称并且它起作用了。因此,只需确认您在函数中没有任何相同的类名
回答by ericdrum
Here's what fixed it for me.
这是为我解决的问题。
I was getting the same warning in some code that I was sure that I typed into the editor initially and allowed it to autocomplete. I subsequently went back and re-visted the warning and tried to just type the same function over again right after my existing function. When I entered the function name over again, my function signature changed and the parms matched up exactly with what Xcode expected and the warning was suppressed.
我在某些代码中收到了相同的警告,我确信我最初在编辑器中输入并允许它自动完成。我随后返回并重新查看警告,并尝试在我现有的函数之后再次输入相同的函数。当我再次输入函数名称时,我的函数签名发生了变化,参数与 Xcode 预期的完全匹配,并且警告被抑制了。
So if you want to do a quick sanity check, do yourself a favor and try typing in the function one more time and see if the parm types change. That might be all you need.
因此,如果您想进行快速的健全性检查,请帮自己一个忙并尝试再次输入该函数,看看参数类型是否发生变化。这可能就是你所需要的。
回答by pipacs
For the record, I had the same problem when implementing WKWebView's didFailProvisionalNavigation delegate method. The solution was to add the @objc declaration andchange the type of the last parameter from Error to NSError:
作为记录,我在实现 WKWebView 的 didFailProvisionalNavigation 委托方法时遇到了同样的问题。解决方案是添加@objc 声明并将最后一个参数的类型从Error 更改为NSError:
@objc(webView:didFailProvisionalNavigation:withError:)
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
// handle error
}
回答by Mark Barrett
Just to add clarification to this rather over complicated workaround: can anyone see why the below is not firing/working when the action is being taken?
只是为了对此进行澄清而不是复杂的解决方法:任何人都可以看到为什么在采取行动时下面没有触发/工作?
extension AppDelegate: UNUserNotificationCenterDelegate {
@objc(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("RESPONSE FROM NOTIFICATION!")
switch response.actionIdentifier {
case "reply":
print("Reply action received!")
case "ignore":
print("Ignore action received!")
default: print("Error - Unknown action received!")
break
}
}
}
回答by GuravS
For xcode 8.1 >= and swift 3,
对于 xcode 8.1 >= 和 swift 3,
add @nonobjc at the beginning of method to silence this warning.
在方法的开头添加 @nonobjc 以消除此警告。