Need to archive CLLocation data

丶灬走出姿态 提交于 2019-12-19 07:56:13

问题


I have an array of CLLocation data that I would like to archive. Should the NSUserDefaults system be used? Otherwise, how best to archive the CLLocation data?


回答1:


UserDefaults can only store certain types of data, and should be used to store user preference information, not arbitrary application state data.

To quote the Apple docs:

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved.

More info here.

If your CLLocation data really does count as user preference info (which I'm not convinced of), you'll have to map the info inside CLLocation onto types that are compatible with NSUserDefaults. Read the docs for NSUserDefaults.

If your CLLocation data isn't user preference info, but just application data/state that you need to preserve, you have a few options; you could store it in core data, or you could use keyed archiving - see this tutorial, for example.




回答2:


To correctly store a CLLocation without loss of information, use an NSKeyedArchiver like this:

CLLocation *myLocationToStore = ...; // (a CLLocation object)
NSData *locationData = [NSKeyedArchiver archivedDataWithRootObject:myLocationToStore];

This can then be archived to NSUserDefaults, and likewise decoded just as simply when you retrieve it with NSKeyedUnarchiver:

CLLocation *myStoredLocation = (CLLocation *)[NSKeyedUnarchiver unarchiveObjectWithData:locationData];

The Swift equivalent functions are:

class func archivedDataWithRootObject(_ rootObject: AnyObject) -> NSData]
class func unarchiveObjectWithData(_ data: NSData) -> AnyObject?



回答3:


Found out the solution using nsarchiver as you said it worked like charm thanks. Here is what i did.

- (id)initWithCoder:(NSCoder *)aDecoder
{

    self.PlaceName=[aDecoder decodeObjectForKey:@"inapPlaceName"];

    float lat, long1;

    lat=[aDecoder decodeDoubleForKey:@"inapCoordinateLat"];

    long1=[aDecoder decodeDoubleForKey:@"inapCoordinateLong"];  

    //inapCoordinate=[[CLLocationCoordinate2D alloc] initWithLatitude:lat1 longitude:long1];
    CLLocationCoordinate2D new_coordinate = { lat, long1 };
    self.inapCoordinate=new_coordinate;

    return self;

}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:PlaceName forKey:@"inapPlaceName"];

    [aCoder encodeDouble:inapCoordinate.latitude forKey:@"inapCoordinateLat"];

    [aCoder encodeDouble:inapCoordinate.longitude forKey:@"inapCoordinateLong"]; 

}



回答4:


This is the usual method.

CLLocation *myLocationToStore = ...; // (a CLLocation object)
NSData *locationData = [NSKeyedArchiver archivedDataWithRootObject:myLocationToStore];


来源:https://stackoverflow.com/questions/5259964/need-to-archive-cllocation-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!