GULUserDefaults.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2018 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. NS_ASSUME_NONNULL_BEGIN
  16. /// A thread-safe user defaults that uses C functions from CFPreferences.h instead of
  17. /// `NSUserDefaults`. This is to avoid sending an `NSNotification` when it's changed from a
  18. /// background thread to avoid crashing. // TODO: Insert radar number here.
  19. @interface GULUserDefaults : NSObject
  20. /// A shared user defaults similar to +[NSUserDefaults standardUserDefaults] and accesses the same
  21. /// data of the standardUserDefaults.
  22. + (GULUserDefaults *)standardUserDefaults;
  23. /// Initializes preferences with a suite name that is the same with the NSUserDefaults' suite name.
  24. /// Both of CFPreferences and NSUserDefaults share the same plist file so their data will exactly
  25. /// the same.
  26. ///
  27. /// @param suiteName The name of the suite of the user defaults.
  28. - (instancetype)initWithSuiteName:(nullable NSString *)suiteName;
  29. #pragma mark - Getters
  30. /// Searches the receiver's search list for a default with the key 'defaultName' and return it. If
  31. /// another process has changed defaults in the search list, NSUserDefaults will automatically
  32. /// update to the latest values. If the key in question has been marked as ubiquitous via a Defaults
  33. /// Configuration File, the latest value may not be immediately available, and the registered value
  34. /// will be returned instead.
  35. - (nullable id)objectForKey:(NSString *)defaultName;
  36. /// Equivalent to -objectForKey:, except that it will return nil if the value is not an NSArray.
  37. - (nullable NSArray *)arrayForKey:(NSString *)defaultName;
  38. /// Equivalent to -objectForKey:, except that it will return nil if the value
  39. /// is not an NSDictionary.
  40. - (nullable NSDictionary<NSString *, id> *)dictionaryForKey:(NSString *)defaultName;
  41. /// Equivalent to -objectForKey:, except that it will convert NSNumber values to their NSString
  42. /// representation. If a non-string non-number value is found, nil will be returned.
  43. - (nullable NSString *)stringForKey:(NSString *)defaultName;
  44. /// Equivalent to -objectForKey:, except that it converts the returned value to an NSInteger. If the
  45. /// value is an NSNumber, the result of -integerValue will be returned. If the value is an NSString,
  46. /// it will be converted to NSInteger if possible. If the value is a boolean, it will be converted
  47. /// to either 1 for YES or 0 for NO. If the value is absent or can't be converted to an integer, 0
  48. /// will be returned.
  49. - (NSInteger)integerForKey:(NSString *)defaultName;
  50. /// Similar to -integerForKey:, except that it returns a float, and boolean values will not be
  51. /// converted.
  52. - (float)floatForKey:(NSString *)defaultName;
  53. /// Similar to -integerForKey:, except that it returns a double, and boolean values will not be
  54. /// converted.
  55. - (double)doubleForKey:(NSString *)defaultName;
  56. /// Equivalent to -objectForKey:, except that it converts the returned value to a BOOL. If the value
  57. /// is an NSNumber, NO will be returned if the value is 0, YES otherwise. If the value is an
  58. /// NSString, values of "YES" or "1" will return YES, and values of "NO", "0", or any other string
  59. /// will return NO. If the value is absent or can't be converted to a BOOL, NO will be returned.
  60. - (BOOL)boolForKey:(NSString *)defaultName;
  61. #pragma mark - Setters
  62. /// Immediately stores a value (or removes the value if `nil` is passed as the value) for the
  63. /// provided key in the search list entry for the receiver's suite name in the current user and any
  64. /// host, then asynchronously stores the value persistently, where it is made available to other
  65. /// processes.
  66. - (void)setObject:(nullable id)value forKey:(NSString *)defaultName;
  67. /// Equivalent to -setObject:forKey: except that the value is converted from a float to an NSNumber.
  68. - (void)setFloat:(float)value forKey:(NSString *)defaultName;
  69. /// Equivalent to -setObject:forKey: except that the value is converted from a double to an
  70. /// NSNumber.
  71. - (void)setDouble:(double)value forKey:(NSString *)defaultName;
  72. /// Equivalent to -setObject:forKey: except that the value is converted from an NSInteger to an
  73. /// NSNumber.
  74. - (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName;
  75. /// Equivalent to -setObject:forKey: except that the value is converted from a BOOL to an NSNumber.
  76. - (void)setBool:(BOOL)value forKey:(NSString *)defaultName;
  77. #pragma mark - Removing Defaults
  78. /// Equivalent to -[... setObject:nil forKey:defaultName]
  79. - (void)removeObjectForKey:(NSString *)defaultName;
  80. #pragma mark - Save data
  81. /// Blocks the calling thread until all in-progress set operations have completed.
  82. - (void)synchronize;
  83. @end
  84. NS_ASSUME_NONNULL_END