xcode PLIST 示例或教程中的 OS X 数据存储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2204093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OS X Data Storage in PLIST Example or Tutorial?
提问by Andrew J. Freyer
I have looked for several hours for a good tutorial on how to store data for my command line program that I am writing in Objective-C. It seems like my options are NSUserDefaults, Plists, sqlite, or XML.
我已经找了几个小时来寻找一个关于如何为我用 Objective-C 编写的命令行程序存储数据的好教程。我的选项似乎是 NSUserDefaults、Plists、sqlite 或 XML。
Now, I think at most I'd have 50kb of data to store, retrieve, and modify, so it seems like from what I read Plists would work just fine, but I'm having difficulty finding a good tutorial online. (I'm completely new to Objective C). Everything I find is for the iPhone, or NSUserDefaults - neither of which I'm looking for.
现在,我认为我最多有 50kb 的数据来存储、检索和修改,所以从我读到的内容来看,Plists 似乎可以正常工作,但我很难在网上找到好的教程。(我对目标 C 完全陌生)。我找到的所有东西都是针对 iPhone 或 NSUserDefaults 的——我都没有寻找。
Can anyone point me in the right direction? I'm hoping just to replicate the very basic functionality of "defaults write com.yourcompanyname.yourappanme key data"
任何人都可以指出我正确的方向吗?我希望只是复制“默认写入 com.yourcompanyname.yourappanme 关键数据”的非常基本的功能
I would greatly appreciate a nudge in the right direction, but again, I'm completely new so the most basic tutorial is best.
我非常感谢朝正确方向的推动,但同样,我是全新的,所以最基本的教程是最好的。
采纳答案by Carl Norum
The Property List Programming Guideis a good place to start.
在属性列表编程指南是一个良好的开端。
The easiest way do do what you want is to put everything you want to save into an NSDictionary
or NSMutableDictionary
, and then use writeToFile:atomically:
to save the data. You can read it back with initWithContentsOfFile:
.
做你想做的最简单的方法是把你想保存的所有东西放到NSDictionary
or 中NSMutableDictionary
,然后用writeToFile:atomically:
来保存数据。你可以用 阅读它initWithContentsOfFile:
。
The property list guide linked above would rather you use NSPropertyListSerialization
to generate an NSData
object from the dictionary, and save that instead. That's a (little) bit more involved, but arguably a better approach.
上面链接的属性列表指南更希望您使用NSPropertyListSerialization
它NSData
从字典中生成一个对象,然后将其保存。这是一个(一点)更多的参与,但可以说是一个更好的方法。
回答by christo16
You actually might want to look into using Core Data: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreData/cdProgrammingGuide.html
您实际上可能想要研究使用核心数据:http: //developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreData/cdProgrammingGuide.html
It will let you save data in XML format.
它将让您以 XML 格式保存数据。