iOS-NSUserDefaults详解及使用

iOS-NSUserDefaults详解及使用



一.NSUserDefaults

NSUserDefaults是轻量级的数据持久化技术,它的主要作用是把比较简单的数据结构实体持久化到本地,如用户名,密码,使用习惯等。就把它当作能够本地化的字典来使用。

二.头文件

+ (NSUserDefaults *)standardUserDefaults; //共享实例对象

+ (void)resetStandardUserDefaults;


- (instancetype)init;

- (instancetype)initWithSuiteName:(NSString *)suitename NS_AVAILABLE(10_9, 7_0) NS_DESIGNATED_INITIALIZER; //nil suite means use the default search list that +standardUserDefaults uses

- (id)initWithUser:(NSString *)username NS_DEPRECATED(10_0, 10_9, 2_0, 7_0);


- (id)objectForKey:(NSString *)defaultName;

- (void)setObject:(id)value forKey:(NSString *)defaultName;

- (void)removeObjectForKey:(NSString *)defaultName;


- (NSString *)stringForKey:(NSString *)defaultName;

- (NSArray *)arrayForKey:(NSString *)defaultName;

- (NSDictionary *)dictionaryForKey:(NSString *)defaultName;

- (NSData *)dataForKey:(NSString *)defaultName;

- (NSArray *)stringArrayForKey:(NSString *)defaultName;

- (NSInteger)integerForKey:(NSString *)defaultName;

- (float)floatForKey:(NSString *)defaultName;

- (double)doubleForKey:(NSString *)defaultName;

- (BOOL)boolForKey:(NSString *)defaultName;

- (NSURL *)URLForKey:(NSString *)defaultName NS_AVAILABLE(10_6, 4_0);


- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName;

- (void)setFloat:(float)value forKey:(NSString *)defaultName;

- (void)setDouble:(double)value forKey:(NSString *)defaultName;

- (void)setBool:(BOOL)value forKey:(NSString *)defaultName;

- (void)setURL:(NSURL *)url forKey:(NSString *)defaultName NS_AVAILABLE(10_6, 4_0);


- (void)registerDefaults:(NSDictionary *)registrationDictionary;


- (void)addSuiteNamed:(NSString *)suiteName;

- (void)removeSuiteNamed:(NSString *)suiteName;


- (NSDictionary *)dictionaryRepresentation;


@property (readonly, copy) NSArray *volatileDomainNames;

- (NSDictionary *)volatileDomainForName:(NSString *)domainName;

- (void)setVolatileDomain:(NSDictionary *)domain forName:(NSString *)domainName;

- (void)removeVolatileDomainForName:(NSString *)domainName;


- (NSArray *)persistentDomainNames NS_DEPRECATED(10_0, 10_9, 2_0, 7_0);

- (NSDictionary *)persistentDomainForName:(NSString *)domainName;

- (void)setPersistentDomain:(NSDictionary *)domain forName:(NSString *)domainName;

- (void)removePersistentDomainForName:(NSString *)domainName;


- (BOOL)synchronize;


- (BOOL)objectIsForcedForKey:(NSString *)key;

- (BOOL)objectIsForcedForKey:(NSString *)key inDomain:(NSString *)domain;

@end



从头文件来看,NSUserDefault主要提供了基本的数据类型的持久化读取方法而已,可以通- (BOOL)synchronize;方法来把数据本地化到本地,在NSUserDefault的使用中都是通过key-value的模式进行操作,从使用思想上来说你就可以把它当作一个可以本地话的字典来操作,通过-(BOOL)synchronize方法来实现本地化。


你可能感兴趣的:(iOS,其他)