需要归档CLLocation数据

发布于 2024-10-21 08:48:49 字数 107 浏览 6 评论 0原文

我有一组想要存档的 CLLocation 数据。是否应该使用 NSUserDefaults 系统?否则,如何最好地归档 CLLocation 数据?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枯寂 2024-10-28 08:48:49

要正确存储 CLLocation 而不丢失信息,请使用 NSKeyedArchiver,如下所示:

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

然后可以将其存档到 NSUserDefaults,并且同样在使用 NSKeyedUnarchiver 检索它时简单地进行解码:

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

Swift 等效函数 是:

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

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?
誰ツ都不明白 2024-10-28 08:48:49

UserDefaults 只能存储某些类型的数据,并且应该用于存储用户首选项信息,而不是任意应用程序状态数据。

引用 Apple 文档< /a>:

NSUserDefaults 类提供了
用于交互的编程接口
使用默认系统。默认值
系统允许应用程序
自定义其行为以匹配
用户的偏好。例如,你
可以让用户确定什么
您的应用程序的测量单位
显示文档的频率或频率
自动保存。

更多信息此处

如果您的 CLLocation 数据确实算作用户首选项信息(我不相信这一点),则必须将 CLLocation 内的信息映射到与 NSUserDefaults 兼容的类型。阅读 NSUserDefaults 文档< /a>.

如果您的 CLLocation 数据不是用户首选项信息,而只是需要保留的应用程序数据/状态,那么您有几个选择;您可以将其存储在核心数据中,也可以使用密钥归档 - 例如,请参阅本教程

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.

原谅我要高飞 2024-10-28 08:48:49

使用 nsarchiver 找到了解决方案,正如您所说,它就像魅力一样,谢谢。这就是我所做的。

- (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"]; 

}

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"]; 

}
多情癖 2024-10-28 08:48:49

这是通常的方法。

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

This is the usual method.

CLLocation *myLocationToStore = ...; // (a CLLocation object)
NSData *locationData = [NSKeyedArchiver archivedDataWithRootObject:myLocationToStore];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文