Xcode 8 Warning “Instance method nearly matches optional requirement”

后端 未结 10 1516
情深已故
情深已故 2020-12-03 05:01

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:

Instance me         


        
相关标签:
10条回答
  • 2020-12-03 05:10

    For xcode 8.1 >= and swift 3,

    add @nonobjc at the beginning of method to silence this warning.

    0 讨论(0)
  • 2020-12-03 05:11

    This had me going around in circles. It was because I created my own Notification class. Once I changed this class name (don't refactor as it will change the objc Notification paramaters), all errors disappeared

    0 讨论(0)
  • 2020-12-03 05:14

    For the record, I had the same problem when implementing WKWebView's didFailProvisionalNavigation delegate method. The solution was to add the @objc declaration and change the type of the last parameter from Error to NSError:

    @objc(webView:didFailProvisionalNavigation:withError:)
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
        // handle error
    }
    
    0 讨论(0)
  • 2020-12-03 05:20

    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:

    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.

    0 讨论(0)
  • 2020-12-03 05:22

    After hours of searching I found this - Swift 3 ObjC Optional Protocol Method Not Called in Subclass

    You can workaround the bug by prefixing the objective-c declaration on the function

    @objc(tableViewSettingsDidChange:notification:)
    func tableViewSettingsDidChange(_ notification:Notification)
    
    0 讨论(0)
  • 2020-12-03 05:23

    We contacted Apple Developer Technical Support (DTS) with this issue. They replied that this is a bug in Xcode 8.

    We submitted a bug report and hope for a quick update. (Apple Bug Report 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.


    Update for Xcode ≥ 8.1

    The problem seems fixed now, at least for the delegate methods we are using in our project.

    0 讨论(0)
提交回复
热议问题