Class NSUserDefaults

  • All Implemented Interfaces:
    NSObject

    public class NSUserDefaults
    extends NSObject
    NSUserDefaults is a hierarchical persistent interprocess (optionally distributed) key-value store, optimized for storing user settings. Hierarchical: NSUserDefaults has a list of places to look for data called the "search list". A search list is referred to by an arbitrary string called the "suite identifier" or "domain identifier". When queried, NSUserDefaults checks each entry of its search list until it finds one that contains the key in question, or has searched the whole list. The list is (note: "current host + current user" preferences are unimplemented on iOS, watchOS, and tvOS, and "any user" preferences are not generally useful for applications on those operating systems): - Managed ("forced") preferences, set by a configuration profile or via mcx from a network administrator - Commandline arguments - Preferences for the current domain, in the cloud - Preferences for the current domain, the current user, in the current host - Preferences for the current domain, the current user, in any host - Preferences added via -addSuiteNamed: - Preferences global to all apps for the current user, in the current host - Preferences global to all apps for the current user, in any host - Preferences for the current domain, for all users, in the current host - Preferences global to all apps for all users, in the current host - Preferences registered with -registerDefaults: Persistent: Preferences stored in NSUserDefaults persist across reboots and relaunches of apps unless otherwise specified. Interprocess: Preferences may be accessible to and modified from multiple processes simultaneously (for example between an application and an extension). Optionally distributed (Currently only supported in Shared iPad for Students mode): Data stored in user defaults can be made "ubiqitous", i.e. synchronized between devices via the cloud. Ubiquitous user defaults are automatically propagated to all devices logged into the same iCloud account. When reading defaults (via -*ForKey: methods on NSUserDefaults), ubiquitous defaults are searched before local defaults. All operations on ubiquitous defaults are asynchronous, so registered defaults may be returned in place of ubiquitous defaults if downloading from iCloud hasn't finished. Ubiquitous defaults are specified in the Defaults Configuration File for an application. Key-Value Store: NSUserDefaults stores Property List objects (NSString, NSData, NSNumber, NSDate, NSArray, and NSDictionary) identified by NSString keys, similar to an NSMutableDictionary. Optimized for storing user settings: NSUserDefaults is intended for relatively small amounts of data, queried very frequently, and modified occasionally. Using it in other ways may be slow or use more memory than solutions more suited to those uses. The 'App' CFPreferences functions in CoreFoundation act on the same search lists that NSUserDefaults does. NSUserDefaults can be observed using Key-Value Observing for any key stored in it. Using NSKeyValueObservingOptionPrior to observe changes from other processes or devices will behave as though NSKeyValueObservingOptionPrior was not specified.
    • Constructor Detail

      • NSUserDefaults

        protected NSUserDefaults​(org.moe.natj.general.Pointer peer)
    • Method Detail

      • accessInstanceVariablesDirectly

        public static boolean accessInstanceVariablesDirectly()
      • allocWithZone

        public static java.lang.Object allocWithZone​(org.moe.natj.general.ptr.VoidPtr zone)
      • automaticallyNotifiesObserversForKey

        public static boolean automaticallyNotifiesObserversForKey​(java.lang.String key)
      • cancelPreviousPerformRequestsWithTarget

        public static void cancelPreviousPerformRequestsWithTarget​(java.lang.Object aTarget)
      • cancelPreviousPerformRequestsWithTargetSelectorObject

        public static void cancelPreviousPerformRequestsWithTargetSelectorObject​(java.lang.Object aTarget,
                                                                                 org.moe.natj.objc.SEL aSelector,
                                                                                 java.lang.Object anArgument)
      • classFallbacksForKeyedArchiver

        public static NSArray<java.lang.String> classFallbacksForKeyedArchiver()
      • classForKeyedUnarchiver

        public static org.moe.natj.objc.Class classForKeyedUnarchiver()
      • debugDescription_static

        public static java.lang.String debugDescription_static()
      • description_static

        public static java.lang.String description_static()
      • hash_static

        public static long hash_static()
      • instanceMethodSignatureForSelector

        public static NSMethodSignature instanceMethodSignatureForSelector​(org.moe.natj.objc.SEL aSelector)
      • instancesRespondToSelector

        public static boolean instancesRespondToSelector​(org.moe.natj.objc.SEL aSelector)
      • isSubclassOfClass

        public static boolean isSubclassOfClass​(org.moe.natj.objc.Class aClass)
      • keyPathsForValuesAffectingValueForKey

        public static NSSet<java.lang.String> keyPathsForValuesAffectingValueForKey​(java.lang.String key)
      • new_objc

        public static java.lang.Object new_objc()
      • resetStandardUserDefaults

        public static void resetStandardUserDefaults()
        +resetStandardUserDefaults releases the standardUserDefaults and sets it to nil. A new standardUserDefaults will be created the next time it's accessed. The only visible effect this has is that all KVO observers of the previous standardUserDefaults will no longer be observing it.
      • resolveClassMethod

        public static boolean resolveClassMethod​(org.moe.natj.objc.SEL sel)
      • resolveInstanceMethod

        public static boolean resolveInstanceMethod​(org.moe.natj.objc.SEL sel)
      • setVersion_static

        public static void setVersion_static​(long aVersion)
      • standardUserDefaults

        public static NSUserDefaults standardUserDefaults()
        +standardUserDefaults returns a global instance of NSUserDefaults configured to search the current application's search list.
      • superclass_static

        public static org.moe.natj.objc.Class superclass_static()
      • version_static

        public static long version_static()
      • URLForKey

        public NSURL URLForKey​(java.lang.String defaultName)
        -URLForKey: is equivalent to -objectForKey: except that it converts the returned value to an NSURL. If the value is an NSString path, then it will construct a file URL to that path. If the value is an archived URL from -setURL:forKey: it will be unarchived. If the value is absent or can't be converted to an NSURL, nil will be returned.
      • addSuiteNamed

        public void addSuiteNamed​(java.lang.String suiteName)
        -addSuiteNamed: adds the full search list for 'suiteName' as a sub-search-list of the receiver's. The additional search lists are searched after the current domain, but before global defaults. Passing NSGlobalDomain or the current application's bundle identifier is unsupported.
      • arrayForKey

        public NSArray<?> arrayForKey​(java.lang.String defaultName)
        -arrayForKey: is equivalent to -objectForKey:, except that it will return nil if the value is not an NSArray.
      • boolForKey

        public boolean boolForKey​(java.lang.String defaultName)
        -boolForKey: is equivalent to -objectForKey:, except that it converts the returned value to a BOOL. If the value is an NSNumber, NO will be returned if the value is 0, YES otherwise. If the value is an NSString, values of "YES" or "1" will return YES, and values of "NO", "0", or any other string will return NO. If the value is absent or can't be converted to a BOOL, NO will be returned.
      • dataForKey

        public NSData dataForKey​(java.lang.String defaultName)
        -dataForKey: is equivalent to -objectForKey:, except that it will return nil if the value is not an NSData.
      • dictionaryForKey

        public NSDictionary<java.lang.String,​?> dictionaryForKey​(java.lang.String defaultName)
        -dictionaryForKey: is equivalent to -objectForKey:, except that it will return nil if the value is not an NSDictionary.
      • dictionaryRepresentation

        public NSDictionary<java.lang.String,​?> dictionaryRepresentation()
        -dictionaryRepresentation returns a composite snapshot of the values in the receiver's search list, such that [[receiver dictionaryRepresentation] objectForKey:x] will return the same thing as [receiver objectForKey:x].
      • doubleForKey

        public double doubleForKey​(java.lang.String defaultName)
        -doubleForKey: is similar to -integerForKey:, except that it returns a double, and boolean values will not be converted.
      • floatForKey

        public float floatForKey​(java.lang.String defaultName)
        -floatForKey: is similar to -integerForKey:, except that it returns a float, and boolean values will not be converted.
      • initWithSuiteName

        public NSUserDefaults initWithSuiteName​(java.lang.String suitename)
        -initWithSuiteName: initializes an instance of NSUserDefaults that searches the shared preferences search list for the domain 'suitename'. For example, using the identifier of an application group will cause the receiver to search the preferences for that group. Passing the current application's bundle identifier, NSGlobalDomain, or the corresponding CFPreferences constants is an error. Passing nil will search the default search list.
      • initWithUser

        @Deprecated
        public NSUserDefaults initWithUser​(java.lang.String username)
        Deprecated.
        -initWithUser: is equivalent to -init
      • integerForKey

        public long integerForKey​(java.lang.String defaultName)
        -integerForKey: is equivalent to -objectForKey:, except that it converts the returned value to an NSInteger. If the value is an NSNumber, the result of -integerValue will be returned. If the value is an NSString, it will be converted to NSInteger if possible. If the value is a boolean, it will be converted to either 1 for YES or 0 for NO. If the value is absent or can't be converted to an integer, 0 will be returned.
      • objectForKey

        public java.lang.Object objectForKey​(java.lang.String defaultName)
        -objectForKey: will search the receiver's search list for a default with the key 'defaultName' and return it. If another process has changed defaults in the search list, NSUserDefaults will automatically update to the latest values. If the key in question has been marked as ubiquitous via a Defaults Configuration File, the latest value may not be immediately available, and the registered value will be returned instead.
      • objectIsForcedForKey

        public boolean objectIsForcedForKey​(java.lang.String key)
        -objectIsForcedForKey: returns YES if the value for 'key' is provided by managed preferences (a configuration profile or mcx)
      • objectIsForcedForKeyInDomain

        public boolean objectIsForcedForKeyInDomain​(java.lang.String key,
                                                    java.lang.String domain)
        -objectIsForcedForKey:inDomain: returns YES if the value for 'key' is provided by managed preferences (a configuration profile or mcx) for the search list named by 'domain'
      • persistentDomainForName

        public NSDictionary<java.lang.String,​?> persistentDomainForName​(java.lang.String domainName)
        -persistentDomainForName: returns a dictionary representation of the search list entry specified by 'domainName', the current user, and any host.
      • persistentDomainNames

        @Deprecated
        public NSArray<?> persistentDomainNames()
        Deprecated.
        -persistentDomainNames returns an incomplete list of domains that have preferences stored in them.
      • registerDefaults

        public void registerDefaults​(NSDictionary<java.lang.String,​?> registrationDictionary)
        -registerDefaults: adds the registrationDictionary to the last item in every search list. This means that after NSUserDefaults has looked for a value in every other valid location, it will look in registered defaults, making them useful as a "fallback" value. Registered defaults are never stored between runs of an application, and are visible only to the application that registers them. Default values from Defaults Configuration Files will automatically be registered.
      • removeObjectForKey

        public void removeObjectForKey​(java.lang.String defaultName)
        -removeObjectForKey: is equivalent to -[... setObject:nil forKey:defaultName]
      • removePersistentDomainForName

        public void removePersistentDomainForName​(java.lang.String domainName)
        -removePersistentDomainForName: removes all values from the search list entry specified by 'domainName', the current user, and any host. The change is persistent.
      • removeSuiteNamed

        public void removeSuiteNamed​(java.lang.String suiteName)
        -removeSuiteNamed: removes a sub-searchlist added via -addSuiteNamed:.
      • removeVolatileDomainForName

        public void removeVolatileDomainForName​(java.lang.String domainName)
      • setBoolForKey

        public void setBoolForKey​(boolean value,
                                  java.lang.String defaultName)
        -setBool:forKey: is equivalent to -setObject:forKey: except that the value is converted from a BOOL to an NSNumber.
      • setDoubleForKey

        public void setDoubleForKey​(double value,
                                    java.lang.String defaultName)
        -setDouble:forKey: is equivalent to -setObject:forKey: except that the value is converted from a double to an NSNumber.
      • setFloatForKey

        public void setFloatForKey​(float value,
                                   java.lang.String defaultName)
        -setFloat:forKey: is equivalent to -setObject:forKey: except that the value is converted from a float to an NSNumber.
      • setIntegerForKey

        public void setIntegerForKey​(long value,
                                     java.lang.String defaultName)
        -setInteger:forKey: is equivalent to -setObject:forKey: except that the value is converted from an NSInteger to an NSNumber.
      • setObjectForKey

        public void setObjectForKey​(java.lang.Object value,
                                    java.lang.String defaultName)
        -setObject:forKey: immediately stores a value (or removes the value if nil is passed as the value) for the provided key in the search list entry for the receiver's suite name in the current user and any host, then asynchronously stores the value persistently, where it is made available to other processes.
      • setPersistentDomainForName

        public void setPersistentDomainForName​(NSDictionary<java.lang.String,​?> domain,
                                               java.lang.String domainName)
        -setPersistentDomain:forName: replaces all values in the search list entry specified by 'domainName', the current user, and any host, with the values in 'domain'. The change will be persisted.
      • setURLForKey

        public void setURLForKey​(NSURL url,
                                 java.lang.String defaultName)
        -setURL:forKey is equivalent to -setObject:forKey: except that the value is archived to an NSData. Use -URLForKey: to retrieve values set this way.
      • setVolatileDomainForName

        public void setVolatileDomainForName​(NSDictionary<java.lang.String,​?> domain,
                                             java.lang.String domainName)
      • stringArrayForKey

        public NSArray<java.lang.String> stringArrayForKey​(java.lang.String defaultName)
        -stringForKey: is equivalent to -objectForKey:, except that it will return nil if the value is not an NSArray. Note that unlike -stringForKey:, NSNumbers are not converted to NSStrings.
      • stringForKey

        public java.lang.String stringForKey​(java.lang.String defaultName)
        -stringForKey: is equivalent to -objectForKey:, except that it will convert NSNumber values to their NSString representation. If a non-string non-number value is found, nil will be returned.
      • synchronize

        public boolean synchronize()
        -synchronize is deprecated and will be marked with the API_DEPRECATED macro in a future release. -synchronize blocks the calling thread until all in-progress set operations have completed. This is no longer necessary. Replacements for previous uses of -synchronize depend on what the intent of calling synchronize was. If you synchronized... - ...before reading in order to fetch updated values: remove the synchronize call - ...after writing in order to notify another program to read: the other program can use KVO to observe the default without needing to notify - ...before exiting in a non-app (command line tool, agent, or daemon) process: call CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication) - ...for any other reason: remove the synchronize call
      • volatileDomainForName

        public NSDictionary<java.lang.String,​?> volatileDomainForName​(java.lang.String domainName)
      • volatileDomainNames

        public NSArray<java.lang.String> volatileDomainNames()
        Volatile domains are not added to any search list, are not persisted, and are not visible to other applications. Using them is not recommended.