Edit: added Dump(lat) Edit2: tried compactMap
I am having trouble retrieving values from an SQL database and plotting longitude and latitude from each row in the database as a new pin on the Map. The problem arises from when I try to retrieve the values from the array
var feedItems: NSArray = NSArray()
func itemsDownloaded(items: NSArray) {
feedItems = items
var i = 0
for selectedLoc in feedItems {
i+=1
let lat = selectedLoc
let lati = 55.74 /// should be gathered from the json extracted from the sql
let longi = 10.88 /// should be gathered from the json
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
mapView.addAnnotation(annotation)
}
}
dump(lat) looks like this:
▿ Name: Optional("1"), Time_in: Optional("2021-11-13 00:00:00"), Latitude: Optional("55.73390° N"), Longitude: Optional(" 12.57243° E") #0
- super: NSObject
▿ name: Optional("1")
- some: "1"
▿ time_in: Optional("2021-11-13 00:00:00")
- some: "2021-11-13 00:00:00"
▿ latitude: Optional("55.73390° N")
- some: "55.73390° N"
▿ longitude: Optional(" 12.57243° E")
- some: " 12.57243° E"
▿ Name: Optional("2"), Time_in: Optional("2021-11-13 00:00:00"), Latitude: Optional("54.73390° N"), Longitude: Optional(" 11.57243° E") #0
- super: NSObject
▿ name: Optional("2")
- some: "2"
▿ time_in: Optional("2021-11-13 00:00:00")
- some: "2021-11-13 00:00:00"
▿ latitude: Optional("54.73390° N")
- some: "54.73390° N"
▿ longitude: Optional(" 11.57243° E")
- some: " 11.57243° E"
▿ Name: Optional("3"), Time_in: Optional("2021-11-13 00:00:00"), Latitude: Optional("56.73390° N"), Longitude: Optional(" 13.57243° E") #0
- super: NSObject
▿ name: Optional("3")
- some: "3"
▿ time_in: Optional("2021-11-13 00:00:00")
- some: "2021-11-13 00:00:00"
▿ latitude: Optional("56.73390° N")
- some: "56.73390° N"
▿ longitude: Optional(" 13.57243° E")
- some: " 13.57243° E"
I just need to retrieve the latitude from this and insert it into the "lati" and " longi" variables. I have tried many many things such as
let lati = lat[latitude]
let lati = lat.latitude
These give the error: "Value of type 'NSArray.Element' (aka 'Any') has no member 'latitude'". I am sure it is just very simple but I am stuck. Can anyone help out a stupid newbie?
let locs = feedItems.compactMap{ Int($0 as! String)}
Resulted in Thread 1:signal SIGABRT
if let lat as? YourObject { let lati = lat.latitude }. BUT, since you are using Swift, in Swift+3, prefers to avoidNSStuffwhen there is a Swift equivalent: NSArray => Array. So:var feedItems: [YourObject] = [YourObject]()you'll avoid the cast then?.NSArrayis a type from Objective-C. It is an untyped array, so we can't tell what's in it. (And neither can the compiler.) It looks like the records in your array are dictionaries of type[String:String?]? (String key and Optional string value). Can you give more information about the structure of your array of lats/longitudes? What does your dump function look like, for example?feedItems, that you usecompactMapto return an array of annotations, and then add them all at once to the map.