IOS中的数据存储 简单总结

  1.  NSKeyedArchiver(加密形式)
  2.  plist
  3.  NSUserDefaults
  4.  writeToFile 
  5.  SQLite3
==== NSKeyedArchiver ========================================
-------CKPerson.h 代码
@interface CKPerson : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
@end
------- CKPerson.m 代码
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init])
    {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
    }
    return self;
}
---------- CKViewController.m
- (IBAction)OnSaveDataClick:(UIButton *)sender {
    CKPerson *p = [[CKPerson alloc] init];
    p.name = @"GoldenKey";
    p.age = 21;
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName = filePath = [docPath stringByAppendingPathComponent:@"student.hehe"];
    [NSKeyedArchiver archiveRootObject:p toFile:filePath]; //保存数据
}
- (IBAction)OnGetDataClick:(UIButton *)sender {
     CKPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; //获取数据
}
==== plist存取 array、dictionary ===================================
- (IBAction)OnArraySaveClick:(UIButton *)sender {
    NSArray *testArray = @[@"111",@"121",@"131",@"141",@"151"];
    [testArray writeToFile:self.path4Array atomically:YES];
}
- (IBAction)OnArrayGetClick:(UIButton *)sender {
    NSArray *testArray = [NSArray arrayWithContentsOfFile:self.path4Array];
    NSLog(@"数组长度为%d",[testArray count]);
}
- (IBAction)OnDictionarySaveClick:(UIButton *)sender {
    NSDictionary *dict = @{@"name":@"key",@"age":@12};
    [dict writeToFile:self.path4Dict atomically:YES];
}
- (IBAction)OnDictionaryGetClick:(UIButton *)sender {
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:self.path4Dict];
    NSLog(@"name:%@",dict[@"name"]);
    NSLog(@"age:%@",dict[@"age"]);
}
==== NSUserDefaults 程序票号设置 =================================
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults setInteger:1 forKey:@"testInt"];
    [userDefaults synchronize];
    
    int i = [userDefaults integerForKey:@"testInt"];
    NSLog(@"i=%d",i);
    //-----------
    
    NSString *name = @"GoldenKey";
    [userDefaults setObject:name forKey:@"name"];
    [userDefaults synchronize];
    
    NSString *nameResult = [userDefaults objectForKey:@"name"];
    NSLog(@"%@",nameResult);
==== writeToFile ============================================
    --读取string----------------------------------------------------
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);
    NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
    
    //第二步:生成在该路径下的文件:
    NSString *FileName=[ourDocumentPath stringByAppendingPathComponent:@"test.hehe"];
    NSString *texts = @"test string";
    NSData *data = [texts dataUsingEncoding:NSUTF8StringEncoding];
    [data writeToFile:FileName atomically:YES];//将NSData类型对象data写入文件,文件名为FileName
    
    
    //读取方式1
    //NSData *dataResult=[NSData dataWithContentsOfFile:FileName options:0 error:NULL];//从FileName中读取出数据
    //NSString *strResult = [[NSString alloc] initWithData:dataResult encoding:NSUTF8StringEncoding];
    
    //读取方式2
    NSString *strResult = [NSString stringWithContentsOfFile:FileName encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",strResult);
    
    //--存取dictionary-------------------------------------------
    
    NSString *dictFileName = [ourDocumentPath stringByAppendingPathComponent:@"dict.hehe"];
    NSDictionary *dictTest = @{@"name":@"GoldenKey",@"age":@24};
    NSData *dictData = [NSJSONSerialization dataWithJSONObject:dictTest options:NSJSONWritingPrettyPrinted error:nil];
    [dictData writeToFile:dictFileName atomically:YES];
    
    NSData *dataResult = [NSData dataWithContentsOfFile:dictFileName];
    NSDictionary *dictResult = [NSJSONSerialization JSONObjectWithData:dataResult options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"name = %@",dictResult[@"name"]);
==== sqlite3 ========================================================
上一篇:To restore the database on a new host-将数据库恢复至一个新的主机上


下一篇:Avoiding PostgreSQL database corruption