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 ========================================================

IOS中的数据存储 简单总结的更多相关文章

  1. iOS中的数据存储

    SQLite3 SQLite3是一款开源的嵌入式关系型数据库,可移植性好,易使用,内存开销小. SQLite3是无类型的,意味着你可以保存任何类型的数据到任意表的任意字段中. SQLite3常用的4种 ...

  2. ios中常见数据存储方式以及SQLite常用的语句

    在iOS中,根据不同的需求对应的有多种数据存储方式: 1.NSUserdefaults  将数据存储到沙盒中(library),方便易用,但是只能存储系统提供的数据类型(plist),不能存储自定义的 ...

  3. iOS中的数据存储方式_SQLite3

    优点: 1) SQLite是一款轻型的嵌入式数据库; 2) 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 3) 它的处理速度比Mysql.PostgreSQL这两款著名的数据库都还 ...

  4. IOS中的数据存储方式,特点,使用情况

    数据存储的核心都是写文件,主要有四种持久化方式:属性列表(Plist),对象序列化,SQLite数据库,CoreData. 存储Plist: 键值进行存储,不能存储对象.对象需要序列化编码才能写入文件 ...

  5. iOS中的数据存储方式_Preference(NSUserDefaults)

    NSUserDefaults适合存储轻量级的本地数据,项目中,我会把一些简单的数据密码.网址.登陆状态BOOL.整型/浮点型数据等和用户有关的数据用它存储.但是它不能存储自定义的对象! 实例化一个 N ...

  6. iOS中的数据存储方式_Plist

    plist文件只能存储OC常用数据类型(NSString.NSDictionary.NSArray.NSData.NSNumber等类型)而不能直接存储自定义模型对象; 我们拿NSData举例: /* ...

  7. iOS中的数据持久化方式

    iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data. 1.属性列表 涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults ...

  8. 67.Android中的数据存储总结

    转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...

  9. IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)

    IOS学习:ios中的数据持久化初级(文件.xml.json.sqlite.CoreData) 分类: ios开发学习2013-05-30 10:03 2316人阅读 评论(2) 收藏 举报 iOSX ...

随机推荐

  1. jQuery ajax在GBK编码下表单提交终极解决方案(非二次编码方法)(转)

    版权声明]:版权归作者所有,转载时请以超链接形式标明文章原始出处和作者信息及本声明:http://www.open-lib.com/Forum/Read_69_1.action 前言: 当jquery ...

  2. River Hopscotch(二分最大化最小值)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9923   Accepted: 4252 D ...

  3. android 安全未来怎么走

  4. ASP.net体系

  5. [爬虫]通过url获取连接地址中的数据

    1. 要想获取指定连接的数据,那么就得使用HtmlDocument对象,要想使用HtmlDocument对象就必需引用using HtmlAgilityPack; 2. 详细步骤如下:     步骤一 ...

  6. Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 2 -使用XQuery 查询XML数据

    XQuery 是一个浏览/返回XML实例的标准语言. 它比老的只能简单处理节点的XPath表达式更丰富. 你可以同XPath一样使用.或是遍历所有节点,塑造XML实例的返回等. 作为一个查询语言, 你 ...

  7. iOS获取运营商信号强度

    此API是apple私有API,所以只可运用在越狱设备中,如果提交appstore,会遭遇apple的拒绝上架反馈! #import <dlfcn.h> int getSignalLeve ...

  8. 条款20:宁以pass-by-reference-to-const替换pass-by-value

    本条款的要点: 1.尽量以pass-by-reference-to-const替换pass-by-value.前者更高效且可以避免切割问题. 2.这条规则并不适用于内建类型及STL中的迭代器和函数对象 ...

  9. 基础算法-查找:线性索引查找(I)

    前面介绍的几种查找的算法都是基于数据有序的基础上进行的.但是在实际的应用中,很多数据集可能有惊人的数据量,面对这些海量的数据,要保证记录全部按照当中的某个关键字有序,其时间代价是非常昂贵的,所以这种数 ...

  10. /etc/group文件详解

    Linux /etc/group文件与/etc/passwd和/etc/shadow文件都是有关于系统管理员对用户和用户组管理时相关的文件.linux /etc/group文件是有关于系统管理员对用户 ...