我有一个应用程序使用与 View Controller 相对应的文件,但是我想在我的程序中包含 userdefaults (NSuserdefaults)。我会在哪里创建它,就像在行中一样
let defaults = NSUserDefaults.standardUserDefaults()
我应该在哪里创建用户默认值?我会把它放在一个单独的文件中,一个 viewcontroller 文件,比如一个开始屏幕吗?或者我会为 NSuserdefualt 创建一个文件并在其他 viewcontroller 文件中调用它吗?
最佳答案
The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.
您可以在源代码的任何地方调用它,因为它是一个单例类。您通常使用它来存储用户设置。
可以引用这个answer , 关于如何在 Swift 3 和 Swift 2.3 中使用它
关于ios - 在哪里使用 Userdefaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531001/