How to store CLLocation using Core Data (iPhone)?

前端 未结 5 1905
故里飘歌
故里飘歌 2021-01-30 01:41

I\'m trying to save a location and retrieve the location on a map afterward using Core Location, MapKit and Core Data frameworks.

What I\'ve done is I just made entity

5条回答
  •  攒了一身酷
    2021-01-30 02:18

    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
    

    More info here.

提交回复
热议问题