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. HashMap和HashTable 学习

    1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key的hash ...

  2. makinacorpus/spynner

    makinacorpus/spynner Intro Contents Intro Credits Companies Authors Contributors Dependencies Feedba ...

  3. BZOJ3231(矩阵连乘,稍有点复杂)

    题目:3231: [Sdoi2008]递归数列 题意: 一个由自然数组成的数列按下式定义:   对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 ...

  4. 在后台获取Textarea控件的值

    使用Request.Form方法 1.在前台设置name 属性 <textarea name="Content">hdjfhjdfhdj</textarea> ...

  5. 【转】android开发中关于模拟器emulation的常见问题

    [转]android开发中关于模拟器emulation的常见问题 Trouble: 无法启动android模拟器,提示 XDM authorization key matches an existin ...

  6. UI界面

    http://www.uimaker.com/uimakerhtml/uidesign/uisoft/2016/0323/122862.html http://www.uimaker.com/uima ...

  7. POJ 3903 Stock Exchange (E - LIS 最长上升子序列)

    POJ 3903    Stock Exchange  (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...

  8. JDK和JRE的差异和区别

    来源:http://docs.oracle.com/javase/7/docs/

  9. WCF跟踪分析 使用(SvcTraceViewer)

    1.首先在WCF服务端配置文件中配置两处,用于记录WCF调用记录! A:<system.serviceModel>目录下: <diagnostics>      <mes ...

  10. Sublime Text 2使用技巧汇总

    一.下载链接: Windows-64bit: http://pan.baidu.com/s/1o6QdKYu 其它版本请移步官网: http://www.sublimetext.com/ 二.破解Li ...