为什么在 iOS 中使用 NSUserDefaults?

发布于 2024-12-26 04:43:44 字数 654 浏览 4 评论 0原文

一般来说,我是 iOS 开发和移动应用程序开发的新手,我对要使用的 Objective C 和 MVC 模式有相当的了解,但我从未做过任何 Mac 开发。

我很难理解 NSUserDefaults 的用途是什么?

我们已经有了像 PList 这样的东西,它可以将数据存储为 XML,并且我们有 SQLite,它是用于此类设备的轻量级数据库。

那我们为什么有它呢?

它是否是我们应用程序的替代简单键值存储,就像我们在云上拥有不同类型的存储(例如 RDBMS 和基于键值的 NoSQL 存储等)一样?

对于 NSUserDefaults,Apple 文档表示:-

“应用程序通过向用户默认数据库中的一组参数分配值来记录此类首选项”

用户的默认数据库是什么意思?

我的猜测是,就像在任何多用户操作系统中一样,我们有各种用户帐户,并且在 Mac 中也以同样的方式,我们可能有多个用户,每个用户都有一个数据库,应用程序将从该数据库加载该用户保存的首选项。

那么,与 Mac OS X 一样,iOS 是否也有多个用户,并且根据登录的 NSUserDefaults 选择他/她的偏好?

如果我错了,请告诉我。

I am new to iOS development and Mobile app development in general, I have decent knowledge about Objective C and MVC patterns to be used and I have never done any Mac development.

I am struggling to understand what NSUserDefaults is for?

We already have something like PList which stores data as XML and we have SQLite which is lightweight DB for such devices.

Then why do we have it?

Is it an alternative simple key-value storage for our app in the same way we have different types of storage on the cloud like an RDBMS and a key-value based NoSQL store etc ?

For NSUserDefaults, Apple docs say that :-

"Applications record such preferences by assigning values to a set of parameters in a user’s defaults database"

What do they mean by user's default database?

My guess is that, like in any multi-user operating system we have various user accounts and in the same way in Mac as well we might be having multiple users each having a database from where applications would load saved preferences for that user.

So like Mac OS X, does iOS also have multiple users and depending upon whichever is logged in NSUserDefaults picks his/her preferences?

Please tell me if I am wrong.

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

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

发布评论

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

评论(5

在巴黎塔顶看东京樱花 2025-01-02 04:43:44

还没有提到的一件事是:NSUserDefaults 支持所有基本标量(float、int、BOOL)类型,以及 plist 兼容类型:NSData、NSString、NSNumber、NSDate、NSArray 和 NSDictionary。 (您提到 plists 作为替代方案 - NSUserDefaults 只是应用程序 Preferences 文件夹中标准 plist 文件的前端。)这使得只需几行代码即可轻松为应用程序状态创建持久存储。更好的是,如果您在模型对象上实现 NSCoding,则可以将它们存档到 NSData 中并将它们放入 NSUserDefaults 中:

NSData* data = [NSKeyedArchiver archivedDataWithRootObject:arrayOfModelObjects];

[[NSUserDefaults standardUserDefaults] setObject:data forKey:kDefaultsKeyModelObjects];

恢复应用程序数据就像

- (void)viewDidLoad
{
    NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:kDefaultsKeyModelObjects];

    arrayOfModelObjects = [[NSKeyedUnarchiver unarchiveRootObject:data] retain];

    // error checking, tell UI to load the new values, etc...
}

(kDefaultsKeyModelObjects 是您在某处定义的字符串常量一样简单。始终将 NSUserDefaults 放在键名错误可能需要几个小时来调试:)

如果您使用 SQLite 等来存储应用程序数据,则必须编写大量代码才能移动。您的模型数据进出数据库。如果您正在处理大量数据、需要高效搜索等,那么这是有意义的;但如果它是网络应用程序中的服务器列表,则将它们放入 NSUserDefaults 中会更容易、更清晰。

最后一件事:NSUserDefaults 非常适合原型设计。即使您知道最终需要 SQLite 存储,您也可以开始使用 NSUserDefaults 作为快速且简单的持久存储,并在编写一堆数据库代码之前让您的 UI 正常工作并充实您的数据模型。

就像 Mac OS X 一样,iOS 也有多个用户吗?
登录 NSUserDefaults 的人会选择他/她的偏好?

不,在 iOS 上,默认 plist 位于应用程序的 Library/Preferences 文件夹中,而不是像 OS X 上那样位于用户特定的文件夹中。我无法想象 iOS 会有多次登录,但你永远不知道。如果是这样,他们就必须为不同的用户创建单独的默认 plist。

One thing that hasn't been mentioned yet: NSUserDefaults supports all basic scalar (float, int, BOOL) types, as well as the plist-compatible types: NSData, NSString, NSNumber, NSDate, NSArray, and NSDictionary. (You mentioned plists as an alternative--NSUserDefaults is just a front-end to a standard plist file in the application's Preferences folder.) This makes it easy to create persistent storage for your application state in just a few lines of code. Even better, if you implement NSCoding on your model objects, you can archive them into an NSData and put them in NSUserDefaults:

NSData* data = [NSKeyedArchiver archivedDataWithRootObject:arrayOfModelObjects];

[[NSUserDefaults standardUserDefaults] setObject:data forKey:kDefaultsKeyModelObjects];

and restoring your app data is as simple as

- (void)viewDidLoad
{
    NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:kDefaultsKeyModelObjects];

    arrayOfModelObjects = [[NSKeyedUnarchiver unarchiveRootObject:data] retain];

    // error checking, tell UI to load the new values, etc...
}

(kDefaultsKeyModelObjects is a string constant you've defined somewhere. Always put your NSUserDefaults keys in one place! Having a typo in a key name can take hours to debug. :)

If you used, e.g., SQLite to store your application data, you have to write a lot of code to move your model data in and out of the database. That makes sense if you're dealing with a lot of data, need efficient searching, etc.; but if it's, say, a list of servers in a networking app, it's easier and cleaner to throw them in NSUserDefaults.

One last thing: NSUserDefaults is great for prototyping. Even if you know you're going to need a SQLite store eventually, you can start out using NSUserDefaults as a quick-and-easy persistent store, and get your UI working and your data model fleshed out before writing a bunch of database code.

So like Mac OS X, does iOS also have multiple users and depending upon
whichever is logged in NSUserDefaults picks his/her preferences ?

Nope, on iOS the defaults plist is in the application's Library/Preferences folder, not in a user-specific folder like it is on OS X. I can't imagine iOS will ever have multiple logins, but you never know. If it did, they'd have to make separate defaults plists for different users.

故事还在继续 2025-01-02 04:43:44

NSUserDefaults 用于保存应用程序正确运行所依赖的一小部分数据,而其他方法则适合存储应用程序使用的大量数据。

NSUserDefaults 变得非常简单,因为您不必担心文件、实体、获取……。这一切都在一条线上完成。

NSUserDefaults are used to save small portions of data the App is dependent on to run correctly, while other approaches are good to store bigger amounts of data, that the app work with.

NSUserDefaults makes is incredible easy, as you don't have to worry about files, entities, fetches,…. It is all done in one line.

我很OK 2025-01-02 04:43:44

它最初是用于存储设置的,但如果 SO 上的问题有任何问题的话,它似乎确实被(滥用)用于很多其他事情。它的“点”在于它为多种数据类型提供了单行数据持久化和检索方法。

iOS 上没有内置于 NSUserDefaults 中的多用户配置(还没有?),但因为 OS X 和 iOS 是基于两个操作系统使用相同的基础、相同的类。

It's originally for storing settings, but does seem to get (ab)used for a lot of other things if the questions on SO are anything to go by. The "point" of it is that it provides a single-line data persistence and retrieval method for a wide range of data types.

There is no multi-user configuration (yet?) on iOS (have you ever had to sign in to your iPhone?) that is built in to NSUserDefaults, but because OS X and iOS are built on the same foundation, the same class is used for both operating systems.

风筝在阴天搁浅。 2025-01-02 04:43:44

iOS 是单用户操作系统。因此,NSUserDefaults 在 OSX 和 iOS 中的使用方式有所不同。

在 iOS 中,您通常使用 NSUserDefaults 在“iOS 默认系​​统”上保存应用程序首选项和/或用户数据。即使您重新启动 iOS 设备,默认系统中保存的数据也将始终保持不变。事实上,即使用户更新了应用程序。

您通常希望使用 NSUserDefaults 来保存应用程序首选项/设置。任何其他海量数据(例如游戏数据或实时计算)应保存在文档目录中的 CoreData 或 sqllite 中。

iOS is a single-user OS. Hence, NSUserDefaults is used differently in OSX and iOS.

In iOS, you normally use NSUserDefaults to save application preferences and/or user data on the "iOS defaults system". Data saved in the defaults system will be persistent at all time even when you reboot the iOS device. As a matter of fact, even when the user updates the application.

You typically want to use NSUserDefaults to save application preferences/settings. Any other massive data such as game data or realtime calculations should be save in CoreData or sqllite in the documents directory.

梦里的微风 2025-01-02 04:43:44

NSUserDefaults 仅为您的应用程序存储值。

注意 - 它并不是要替代传统的数据存储(核心数据、SQLite、属性列表等用于存储应用程序数据)。 NSUserDefaults 的用途是针对特定的键值对。

例如,登录过程完成后,我用它来存储经过身份验证的用户的网络 ID 和全名。当会话超时或注销时,我会清除这些值。为此,NSUserDefaults 闪闪发光。

NSUserDefaults stored values for only your application.

Note - It is not meant to serve as a replacement for a traditional store for data (Core Data, SQLite, Property Lists, etc are meant for storing application data). What NSUserDefaults is meant for is for specific key value pairs.

For example, I use it to store an authenticated user's networkid and full name once the login process is done. Upon session timeout or logout, I clear those values. For this NSUserDefaults shines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文