IOS中的数据存储 简单总结
2016-02-24 21:08
489 查看
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 ========================================================
==== 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 ========================================================
相关文章推荐
- iOS 开发中用户记住账户,密码
- Missing iOS Distribution signing identity for ...
- iOS 创建单例的两种方法
- iOS中的应用启动原理
- 【转】【iOS测试系列】常用测试小插件的使用
- iOS 设置group类型tableview的section间距
- IOS开发学习之OC的消息机制。
- iOS 开发小结
- ios学习----------SDWebImage框架解析(3)
- ios 如何通过XIB 或者storyboard 创建headView 和footerView?
- ios 将颜色转化为图片
- nagios监控环境搭建
- IOS学习记录之Protocol和delegate
- ios 暂无聊天消息设置
- 获取验证码模型-ios
- iOS多线程的初步研究(四)-- NSTimer
- iOS多线程dispatch_async
- iOS开发 高级绘图
- iOS 开发的几个有用小技巧
- IOS:Block的生命周期