Reputation: 673
Perhaps I can't find an answer to this question because it's something so very simple that I should be able to figure it out, but I'm stumped. Oh, and apologies if some of my terminology is off - I'm still learning.
I'm using Swift and have an array derived from Core Data. So far, so good. Two elements within this array are Doubles/NSNumbers storing latitude and longitude. I'll be using these two elements to draw on to a map, but I can't figure out how to get these two into an array of CLLocations of their own.
So, I've got my array of all the data in Core Data:
var locationsList: [Locations] = []
var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
var request = NSFetchRequest(entityName: "Locations")
let pred = NSPredicate(format: "game = %d", passedGameNumber)
request.predicate = pred
request.sortDescriptors = [NSSortDescriptor(key:"time", ascending: false)]
self.locationsList = context.executeFetchRequest(requestMap, error: nil)! as [Locations]
But it holds data from everything in Core Data (filtered by game):
class Locations: NSManagedObject {
@NSManaged var game: NSNumber
@NSManaged var time: NSDate
@NSManaged var latitude: NSNumber
@NSManaged var longitude: NSNumber
}
And I only need an array containing latitude and longitude, which I will need to convert to CLLocation to put on a map. I should have the map part figured out - it's just this array that has me scratching my head! Thanks.
Upvotes: 1
Views: 5337
Reputation: 1070
You don't need to use NSNumber to store latitude and longitude points. You can store CLLocation directly to core data instead.
Set up an entity for each CLLocation, it'll be a too-many relation off whatever entity is using location points. Let's call this LocationPoint:
class LocationPoint: NSManagedObject {
@NSManaged var game: NSNumber
@NSManaged var time: NSDate
@NSManaged var location: AnyObject
}
You then set the location property to transformable in the Xcode data model. That's it!
In Objective-c you can actually still declare this LocationPoint property as CLLocation without any errors:
@property (nonatomic, strong) CLLocation *location
Upvotes: 0
Reputation: 1713
Good solution Adam...I was able to do something similar like this..
Declare these global variables
var longitudeCollection: [String] = [String]()
var latitudeCollection: [String] = [String]()
Make an extension to easily access CLLoationDegrees type as a Double
extension String {
var coordinateValue: CLLocationDegrees {
return (self as NSString).doubleValue
}
}
Then this is how you would append the coordinates
for var index = 0; index<=longitudeCollection.count-1; index++ {
var lat = latitudeCollection[index].coordinateValue
var long = longitudeCollection[index].coordinateValue
var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long)
coordinates.append(coordinatesToAppend)
}
Upvotes: 1
Reputation: 673
Figured it out. On top of a for loop for iterating through the Core Data array, I also had to convert my latitude and longitudes to Doubles before appending them to my new array as CLLocationCoordinate2D.
var coordinates: [CLLocationCoordinate2D] = []
for index in 0..<self.locationsList.count{
var lat = Double(self.locationsList[index].latitude)
var long = Double(self.locationsList[index].longitude)
var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long)
coordinates.append(coordinatesToAppend)
}
Upvotes: 5