Understanding the UserDefaults register method

前端 未结 1 1007
借酒劲吻你
借酒劲吻你 2020-12-31 06:16

First of all, let me say that this is NOT a complaint, I just want to understand how does the register(defaults: []) method works.

I\'m using the UserDe

1条回答
  •  清酒与你
    2020-12-31 06:45

    From the documentation for register(defaults:):

    If there is no registration domain, one is created using the specified dictionary, and NSRegistrationDomain is added to the end of the search list.

    The contents of the registration domain are not written to disk; you need to call this method each time your application starts.

    What this means is that the registered defaults act as a fallback to the normal, user defaults that you are working with. The registered defaults do not overwrite or replace the standard user defaults.

    When you attempt to lookup a key from UserDefaults.standard, if the key isn't there, then it is looked up in the registered defaults and that result (if any) is returned.

    Once you set a value in UserDefaults.standard, of course it is that value that is returned for the key. If you remove the key from UserDefaults.standard then the registered default is used again.

    0 讨论(0)
提交回复
热议问题