iOS資料儲存持久化(plist,偏好設定,歸檔)
1,plist
使用plist儲存資料,首先要指定路徑(一般在Document路徑下),plist只支援以下的型別:
NSArray;
NSMutableArray;
NSDictionary;
NSMutableDictionary;
NSData;
NSMutableData;
NSString;
NSMutableString;
NSNumber;
NSDate;
2.preference偏好設定(也就是經常說的Userdefault)
偏好設定是專門用來儲存應用程式的配置資訊的,一般不要在偏好設定中儲存其他資料。
如果沒有呼叫synchronize方法,系統會根據I/O情況不定時刻地儲存到檔案中。所以如果需要立即寫入檔案的就必須呼叫synchronize方法。
3.歸檔
NSCoding協議聲明瞭兩個方法,這兩個方法都是必須實現的。一個用來說明如何將物件編碼到歸檔中,另一個說明如何進行解檔來獲取一個新物件。
遵循協議和設定屬性
//1.遵循NSCoding協議
@interface Person : NSObject //2.設定屬性
@property (strong, nonatomic) UIImage *avatar;
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) NSInteger age;
@end
實現協議方法
//解檔
- (id)initWithCoder:(NSCoder *)aDecoder {
if ([super init]) {
self.avatar = [aDecoder decodeObjectForKey:@”avatar”];
self.name = [aDecoder decodeObjectForKey:@”name”];
self.age = [aDecoder decodeIntegerForKey:@”age”];
}
return self;
}
//歸檔
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.avatar forKey:@”avatar”];
[aCoder encodeObject:self.name forKey:@”name”];
[aCoder encodeInteger:self.age forKey:@”age”];
}
具體實現
plist
NSString*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject
//儲存
-(void)SaveDataWithFileName:(NSString )filename InserteNSArray:(NSArray )insertearray{
// NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *fileName = [Path stringByAppendingPathComponent:filename];
[insertearray writeToFile:fileName atomically:YES];
}
//讀取
-(NSArray )seleteDataWithFileName:(NSString )filename{
NSString *fileName = [Path stringByAppendingPathComponent:filename];
NSArray *result = [NSArray arrayWithContentsOfFile:fileName];
return result;
}
偏好設定
-(void)SaveDataWithPreference:(NSDictionary )insertedictionary KeyName:(NSString )keyname{
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:insertedictionary[keyname] forKey:keyname];
[userdefaults synchronize];//最好設定為同步
}
-(NSString )getDataWithPreference:(NSString )keyname{
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
NSString *value = [userdefaults objectForKey:keyname];
return value;
}
歸檔
.h檔案
@interface testCoder : NSCoder
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *male;
@property (nonatomic,assign)int age;
@property (nonatomic,strong)NSArray *arr;
@end
.m檔案
@implementation testCoder
//解檔
- (id)initWithCoder:(NSCoder *)aDecoder {
if ([super init]) {
self.male = [aDecoder decodeObjectForKey:@”male”];
self.name = [aDecoder decodeObjectForKey:@”name”];
self.age = [aDecoder decodeIntForKey:@”age”];
self.arr = [aDecoder decodeObjectForKey:@”arr”];
}
NSLog(@"解檔解檔解檔解檔");
return self;
}
//歸檔
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.male forKey:@”male”];
[aCoder encodeObject:self.name forKey:@”name”];
[aCoder encodeInteger:self.age forKey:@”age”];
[aCoder encodeObject:self.arr forKey:@”arr”];
NSLog(@"歸檔歸檔歸檔歸檔");
}
@end
呼叫
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@”person.data”];
//儲存
for (NSDictionary *dict in array) {
testCoder *testcoder = [[testCoder alloc] init];
testcoder.male = dict[@"male"];
testcoder.name = dict[@"name"];
testcoder.age = [dict[@"age"]intValue];
testcoder.arr = array;
[NSKeyedArchiver archiveRootObject:testcoder toFile:file];
}
//讀取
testCoder *person = [NSKeyedUnarchiver unarchiveObjectWithFile:file];