NSUserDefaulnts中可以存儲(chǔ)NSData,NSString,NSNUmber,NSDate,NSArray,NSDictionary這些實(shí)例,同時(shí)NSUserDefaults是單例携取,同時(shí)也是線程安全的咐低。
你可以在程序運(yùn)行的時(shí)候從用戶默認(rèn)的數(shù)據(jù)庫(kù)中讀取程序的設(shè)置。同時(shí)NSUserDefaults的緩存避免了在每次讀取數(shù)據(jù)時(shí)候都打開用戶默認(rèn)數(shù)據(jù)庫(kù)的操作筏勒≡羯可以通過調(diào)用synchronize方法來使內(nèi)存中的緩存與用戶默認(rèn)系統(tǒng)進(jìn)行同步顾瞻。
也可以自定義存儲(chǔ)一些NSData,NSUserDefaulnts取出時(shí)都是不可變數(shù)據(jù),即使你存入的是可變數(shù)組,取出時(shí)也是不可變的額.
存儲(chǔ)自定義的對(duì)象
.h
@interfaceMyObject : NSObject
{
NSNumber*lowValue;
NSNumber*highValue;
NSString*titleString;
}
@property(nonatomic, retain)NSNumber*lowValue;
@property(nonatomic, retain)NSNumber*highValue;
@property(nonatomic, retain)NSString*titleString;
.m
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.lowValue forKey:@"lowValue"];
[encoder encodeObject:self.highValue forKey:@"highValue"];
[encoder encodeObject:self.titleString forKey:@"titleString"];
}- (id)initWithCoder:(NSCoder *)decoder
{if(self =[super init])
{
self.lowValue= [decoder decodeObjectForKey:@"lowValue"];
self.highValue= [decoder decodeObjectForKey:@"highValue"];
self.titleString= [decoder decodeObjectForKey:@"titleString"];
}returnself;
}