Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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'

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'

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.

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

like image 704
andrejbroncek Avatar asked Apr 13 '15 17:04

andrejbroncek


2 Answers

I'd say your problem is the ExtendedMarker type 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 GMSMarker or any subclass thereof.

Your method does not satisfy the interface contract because it only accepts instances of ExtendedMarker - which I assume is a subclass of 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.

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
}
like image 59
JeremyP Avatar answered Nov 04 '22 09:11

JeremyP


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)

like image 1
Vik Avatar answered Nov 04 '22 09:11

Vik