从启动到启动存储 NSStrings(或其他数据)的快速而肮脏的方法?
我想允许我的用户存储自定义短语,以显示在可编辑的 UITableView 中。
存储这些字符串的方法是怎样的又快又脏呢?
我对 iPhone 开发还很陌生。我了解 Core Data,但不知道如何使用它。如果可能的话,我会为了这个特定的项目而远离这种情况。这里可以使用 PLIST 文件吗?
示例代码值得赞赏。
I want to allow my user to store custom phrases, to be displayed in an editable UITableView.
What's a quick and dirty to store these strings?
I'm fairly new at iPhone development. I know about Core Data, but not how to use it. I would stay away form that just for this particular project if possible. Are PLIST files a possibility here?
Sample code is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要
NSDefaults
;查看用户默认编程主题指南。You probably want
NSDefaults
; check out the User Defaults Programming Topics guide.使用 NSUserDefaults 。 这个问题中有一些代码。
Use NSUserDefaults. There's some code over in this question.
如果你想使用 plists,这是最快的方法(我认为)。还有其他方法可以使用 NSCoding 将数据编码到文件中,但您可能需要一个自定义数据模型类,这更适合保存更多不适合规定数据模型的随机事物。另外,正如已经指出的,NSUserDefaults 也是一个选项
要保存您的工作:
并恢复您保存的文件:
基本上,当您的应用程序即将退出时,您可以创建您想要的对象的数组保存并将它们写入文件,当应用程序再次启动时,您可以将该文件读回到数组中,并根据保存它们的顺序分配变量。然后您进行设置,以便当您的应用程序发送其 UIApplicationWillTerminateNotification 时,它会执行您的代码以将(大概)修改后的变量保存回文件中。
If you want to use plists this is the quickest way (I think) to go. There are other ways to do it with NSCoding to encode your data to a file, but you would probably need a custom data model class, this is more suited to saving more random things that wouldn't fit into a prescribed data model. Also, as has been pointed out, NSUserDefaults is also an option
To save your work:
And to recover your saved file:
Basically, when your application is about to quit you create and array of the objects you want to save and write them to the file, and when you application starts again you read that file back into an array and assign your variables based on the order in which you saved them. Then you set it up so when your application sends its UIApplicationWillTerminateNotification it executes your code to save your (presumably) modified variables back into a file.