2

I am getting following error in compiler -

(position: CLLocationCoordinate2D) -> GMSMarker is not convertible to (position: CLLocationCoordinate2D) -> GMSMarker!.

Please help me to resolve it.

The code that gives this error is -

let location = CLLocationCoordinate2D(latitude: Double(latitudeVal!)!, longitude: Double(longitudeVal!)!)
let locationMarker = GMSMarker(position: location)

Where latitudeVal & longitudeVal are string values from server.

Thanks in advance.

2
  • Have you tried if let? like: if let foo = (position: CLLocationCoordinate2D) {//code} Commented May 2, 2016 at 11:15
  • Take a look at this Q&A that I co-wrote on how to safely deal with optionals. Commented May 2, 2016 at 11:32

5 Answers 5

1

This is a bug of the Swift compiler optimization. See 'UIFont' is not convertible to 'UIFont?'.

To fix this, you can turn off 'Whole Module Optimization' in Build Settings -> Optimization Level -> (Debug/Release).

Alternatively, you can instead change your code to the following without turning off the 'Whole Module Optimization'.

let locationMarker = GMSMarker.init(position: location)

Hope this help.

Sign up to request clarification or add additional context in comments.

Comments

0

Apply below code, its working fine at my side.

let location = CLLocationCoordinate2DMake((latitudeVal as NSString).doubleValue, (longitudeVal as NSString).doubleValue)
let locationMarker = GMSMarker(position: location)

Hope this will work for you.

1 Comment

I have tried all of the above methods, none worked. I have the same code in 3 places and it shows compiler error on 2 places. I don't understand whats wrong. The latitude,longitude values are accessed from models - eg.city.lat. In 3 places 3 different models are used.
0

the equation let locationMarker = GMSMarker(position: location), left of equation is type of GMSMarker!, right of the equation is type of GMSMarker. So you can not assign the right to the left。 you can change like let locationMarker: GMSMarker = GMSMarker(position: location) or let locationMarker = GMSMarker(position: location)!. you can try.

Comments

0

You should unwrap your optionals.

    guard let long = longitudeVal else {
        print("No longitude provided")
        return
    }

    guard let lat = latitudeVal else {
        print("No latitude provided")
        return
    }

    guard let longVal = Double(long) else {
        print("Longitude contains an invalid value")
        return
    }

    guard let latVal = Double(lat) else {
        print("Latitude contains an invalid value")
        return
    }

    let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    let locationMarker = GMSMarker(position: location)

Take a look at Early Exit

Comments

0

Ok, finally i solved it :) :-

let locationMarker = GMSMarker()
locationMarker.position = location

Hope it won't make any difference in the working of the code.

Thank you all!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.