Need to archive CLLocation data

前端 未结 4 703
迷失自我
迷失自我 2021-01-12 21:37

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

4条回答
  •  萌比男神i
    2021-01-12 21:52

    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?
    

提交回复
热议问题