应用程序退出后保留用户名 nsstring
您认为在应用程序中存储简单的用户名字符串的正确方法是什么,以便您再次打开应用程序时它仍然存在?我一直在阅读几种方法来做到这一点:核心数据、plist、nsuserdefaults。
人们使用哪些方法来做到这一点?如果可能的话,所提议的方法的优点和缺点是什么?
What would you consider the proper way of storing a simple username string in your app, so that it is there when you open the app again? I've been reading of a couple of ways to do so: core data, plist, nsuserdefaults.
What are some of the ways people are using to do this, and if possible pros and cons of the proposed way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为 NSUserDefaults 是正确的选择。它用于存储用户的默认设置,这正是您所描述的。此外,从您的代码中访问它非常容易。
I think NSUserDefaults would be the right choice here. It's there for storing the user's default settings, which is exactly what you describe. In addition, it is very easy to access from your code.
NSUserDefaults 没问题。但是,我想如果您有用户名,那么您也有登录密码,不是吗?既然这样,为什么不使用Keychain来保存呢?
NSUserDefaults is OK. However, I guess that if you have a user name, you also have a password to log in, don't you? In such case, why don't you use Keychain then to save it?
核心数据被序列化到 SQLite 中,当您处理许多对象并动态创建/删除它们时更有意义。
Plist 的复杂性介于 Core Data 和 NSUserDefaults 之间。它们可以处理具有某种结构的数据,并作为文件保存在文件系统上。
由于您只处理一条信息,因此 NSUserDefaults 对您来说最有意义。保存字符串所需要做的就是:
然后,检索字符串:
Core Data is serialized into SQLite, and makes more sense for when you're dealing with many objects and dynamically creating/deleting them.
Plists fall somewhere between the complexity of Core Data and NSUserDefaults. They can handle data with some structure, and are saved as a file on the filesystem.
Since you're just dealing with one piece of information, NSUserDefaults makes the most sense for you. All you have to do to save a string is:
Then, to retrieve the string:
NSUserDefaults 是最好的(在我看来)。但是,如果您决定不使用存档,也可以使用它。
也就是说,如果您使用 UIDocument,并且您希望在不同应用程序上同步用户名,则可以将其链接到云。
NSUserDefaults is the best (in my opinion). However you could also use archiving if you decided not to use it.
That said if you use a UIDocument you can link it to the cloud if you what the usernames to be synchronised over different apps.