OSD
OSD

Reputation: 131

Swift optional issue

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.

Upvotes: 2

Views: 209

Answers (5)

Eddie Lau
Eddie Lau

Reputation: 668

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.

Upvotes: 1

OSD
OSD

Reputation: 131

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!

Upvotes: 0

Laffen
Laffen

Reputation: 2771

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

Upvotes: 0

rose
rose

Reputation: 241

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.

Upvotes: 0

Jigar Tarsariya
Jigar Tarsariya

Reputation: 3237

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.

Upvotes: 0

Related Questions