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. SVN同步出现故障

    1.错误描写叙述    同步SVNStatusSubscribe时报告了错误,1中的0个资源已经同步    同步/frame时错误发生:Error getting status for resourc ...

  2. iOS圆盘转动引导图的简单实现

    最近更新的一批app,好多都采用了圆盘转动的效果,比如:百度音乐.当当,大概效果如下: 看看这个是怎么实现的吧. 一.视图元素布局 首先需要明确,这些视图元素是分布在一个圆周上的,通过滑动位置,以圆周 ...

  3. Jenkins持续集成相关文章整理

    构建iOS持续集成平台(一)——自动化构建和依赖管理 构建iOS持续集成平台(二)——测试框架 构建iOS持续集成平台(三)——CI服务器与自动化部署 使用Jenkins搭建iOS开发的CI服务器 一 ...

  4. Raspberry 3安装docker

    SD卡制作 准备一张4GB或者以上的micro sd卡,下载系统镜像,例如raspbian-jessie-lite.img,并使用刷机工具,如Pi filler将其写入sd卡,当然也可以使用命令行的d ...

  5. Canvas使用渐变之-线性渐变详解

    在canvas里面,除了使用纯色,我们还能把填充和笔触样式设置为渐变色:线性渐变和径向渐变. 线性渐变 createLinearGradient(x0,y0,x1,y1)  返回 CanvasGrad ...

  6. ThinkPHP第五天(提交类型判定常量IS_POST等,错误页面种类,Model实例化方式,模板中使用函数,foreach循环,模板中.语法配置)

    1.IS_GET.IS_POST.IS_PUT.IS_DELETE.IS_AJAX常量,方便快捷实现各个判断. 在Action类中还可以使用$this->isPost()等进行判断. 2.错误页 ...

  7. spoj 3871 gcd extreme

    题目大意给出一个n,求sum(gcd(i,j),<i<j<=n); 可以明显的看出来s[n]=s[n-]+f[n]; f[n]=sum(gcd(i,n),<i<n); 现 ...

  8. C语言之一数三平方

    一数三平方 有这样一个六位数,它本身是一个整数的平方,其高三位和低三位也分别是一个整数的平方,如225625=475*475,225=15*15,625=25*25;统计所有符合该条件的六位数 源代码 ...

  9. 编程的毛病——C++之父访谈

    原文见:http://www.technologyreview.com/InfoTech/17831/  翻译:xeon 11/29/2006 在20世纪的80年代和90年代,Bjarne Strou ...

  10. 数据结构——二叉树(Binary Trees)

    非线性数据结构 二叉搜索树(Binary Search Tree) 树的密度=结点数/高度 二叉树类 #pragma once class stnode { public: int nodeValue ...