Kelvin Tan
Kelvin Tan

Reputation: 347

Contextual Type AnyObject Cannot Be Used With Array Literal for Coordinates

I have look into several other different questions posted with the same problem but non of them help answer my question because I am using this for coordinates. I recently upgrade to Swift3 and I got this error "Contextual Type AnyObject Cannot Be Used With Array Literal"

So this is how the code looks like where I declare my variable for my coordinates

 var coordinates: [AnyObject]!

This is the next code where the error occurs, I will replace the numbers with x. I am using the longitude and latitude

coordinates = [[xx.xxxxxx, -xxx.xxxxxx],[xx.xxxxxx, -xxx.xxxxxx],[xx.xxxxxx, -xxx.xxxxxx]]

And the way I call it is by using for loop which I dont think causes any problem but I'll just post it just in case

for i in 0...2
    {
        let coordinate = coordinates[i]}

Problem solved by implementing

var coordinates: [[Double]]!

Upvotes: 0

Views: 812

Answers (1)

Dravidian
Dravidian

Reputation: 9945

Just change :-

import MapKit


class yourController : ...{  

var coordinates: [AnyObject]!

To

var coordinates = [CLLocationCoordinate2D]()

To access a particular lat or long at a particular index just:-

coordinates[your_index].latitude // Latitude

coordinates[your_index].longitude //Longitude

CLLocationCoordinate2D is a struct , much more easily scalable and accessible than array.

Upvotes: 0

Related Questions