更新到 XCode 6.3 (Swift 1.2) 后出现“Objective-C 方法与可选要求方法冲突”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29611312/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:58:50  来源:igfitidea点击:

"Objective-C method conflicts with optional requirement method"error after update to XCode 6.3 (Swift 1.2)

iosobjective-cxcodeswift

提问by andrejbroncek

I am using Google Maps iOS SDK in my app, everything worked great until today. I have downloaded Xcode 6.3 and got a few errors. Sorted out all of them, except for two errors in my MapViewController class, that popped up on these two methods:

我在我的应用程序中使用 Google Maps iOS SDK,直到今天一切都很好。我已经下载了 Xcode 6.3 并遇到了一些错误。整理了所有这些,除了我的 MapViewController 类中的两个错误,它们在这两个方法上弹出:

first method:

第一种方法:

func mapView(mapView: GMSMapView!, didTapMarker marker: ExtendedMarker!) -> Bool {
    ... some code ...
}

with error:

有错误:

Objective-C method 'mapView:didTapMarker:' provided by method 'mapView(:didTapMarker:)' conflicts with optional requirement method 'mapView(:didTapMarker:)' in protocol 'GMSMapViewDelegate'

由方法“mapView( :didTapMarker:)”提供的Objective-C 方法“mapView:didTapMarker:”协议“GMSMapViewDelegate”中的可选要求方法“mapView(:didTapMarker:)”冲突

second method:

第二种方法:

func mapView(mapView: GMSMapView!, markerInfoContents marker: ExtendedMarker!) -> UIView! {
    ... some code ...
}

with error:

有错误:

Objective-C method 'mapView:markerInfoContents:' provided by method 'mapView(:markerInfoContents:)' conflicts with optional requirement method 'mapView(:markerInfoContents:)' in protocol 'GMSMapViewDelegate'

由方法“mapView( :markerInfoContents:)”提供的Objective-C 方法“mapView:markerInfoContents:”协议“GMSMapViewDelegate”中的可选要求方法“mapView(:markerInfoContents:)”冲突

I tried rewriting those methods, but it did not help. I also checked for an update on Google Maps SDK, but last update is from February 2015.

我尝试重写这些方法,但没有帮助。我还检查了 Google Maps SDK 的更新,但最后一次更新是 2015 年 2 月。

I would be thankful for any advice, thank you in advance! :)

如有任何建议,我将不胜感激,在此先感谢您!:)

采纳答案by JeremyP

I'd say your problem is the ExtendedMarkertype for the second parameter. By adopting the protocol, your class promises that, if it implements the optional method mapView:didTapMarker:, the second parameter can be a GMSMarkeror any subclass thereof.

我会说你的问题是ExtendedMarker第二个参数的类型。通过采用该协议,您的类承诺,如果它实现了可选方法mapView:didTapMarker:,则第二个参数可以是 aGMSMarker或其任何子类。

Your method does not satisfy the interface contract because it only accepts instances of ExtendedMarker- which I assume is a subclass of GMSMarker.

您的方法不满足接口契约,因为它只接受的实例ExtendedMarker- 我假设它是GMSMarker.

I would define the method something like this. You need to be prepared to deal with non ExtendedMarker instances being passed in because the contract says you might get them. Simply trying to force the cast may cause an exception.

我会像这样定义方法。您需要准备好处理传入的非 ExtendedMarker 实例,因为合同说您可能会得到它们。简单地尝试强制转换可能会导致异常。

func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool 
{
    // Non specific ExtendedMarker processing

    if let marker = marker as? ExtendedMarker
    {     
        // ExtendedMarker specific processing
    }
    // More non specific ExtendedMarker processing
}

回答by Vik

Unfortunately I don't have the Google iOS SDK at hand, but could it be that the error is because of the parameters marked as force-unwrapped? Maybe the force-unwrap is not required anymore (I had a similar issue with another method when migrating to Swift 1.2, so just guessing)

不幸的是,我手头没有 Google iOS SDK,但是错误可能是由于标记为强制解包的参数造成的吗?也许不再需要强制解包(我在迁移到 Swift 1.2 时遇到了与另一种方法类似的问题,所以只是猜测)

回答by ilias

I had the same issue with 'didTapInfoWindowOfMarker'. If you try the following code it might work for you as well:

我对“didTapInfoWindowOfMarker”有同样的问题。如果您尝试以下代码,它也可能对您有用:

func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
     let placeMarker = marker as! ExtendedMarker
    ... some code ...
}

you can do the same with the other one as well. I hope it will work for you!

你也可以对另一个做同样的事情。我希望它对你有用!