需要归档CLLocation数据
我有一组想要存档的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要正确存储 CLLocation 而不丢失信息,请使用 NSKeyedArchiver,如下所示:
然后可以将其存档到 NSUserDefaults,并且同样在使用 NSKeyedUnarchiver 检索它时简单地进行解码:
Swift 等效函数 是:
To correctly store a CLLocation without loss of information, use an NSKeyedArchiver like this:
This can then be archived to NSUserDefaults, and likewise decoded just as simply when you retrieve it with NSKeyedUnarchiver:
The Swift equivalent functions are:
UserDefaults 只能存储某些类型的数据,并且应该用于存储用户首选项信息,而不是任意应用程序状态数据。
引用 Apple 文档< /a>:
更多信息此处。
如果您的 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:
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.
使用 nsarchiver 找到了解决方案,正如您所说,它就像魅力一样,谢谢。这就是我所做的。
Found out the solution using nsarchiver as you said it worked like charm thanks. Here is what i did.
这是通常的方法。
This is the usual method.