IOS中数据的存储方式总结 (待续。。。。。)

来源:互联网 发布:改属性怎么用SQL 编辑:程序博客网 时间:2024/05/03 12:18

IOS数据的存储涉及的知识有


plist,Preference(使用NSUserDefaults),NSKeyedAchiver,SQLite3,Core Data


数据持久化之.plist文件

文件目录简单说明:

  • 应用程序包:包含了所有的资源文件和可执行文件
  • Document:保存应用运行时生成的需要持久化的数据,iTunes 同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录
  • tmp:保存应用运行时所需的临时数据,使用完毕后再将相应地文件从该目录删除,应用没有运行时,系统也可能会清除该目录下得所有文件。iTunes 同步设备时不会备份该目录。
  • Library / Caches:保存应用运行时生成的需要持久化的数据,iTunes 同步设备时不会备份该目录。一般存储体积大,不需要备份的非重要数据。
  • Library / Preference:保存应用的所有偏好设置,ios 的 Setting (设置)应用会在该目录中查找应用的设置信息。iTunes 同步设备时会备份该目录。

下面是利用字典将数据写入到.plist文件

[cpp] view plaincopyprint?
  1. //  
  2. //  ViewController.m  
  3. //  plist  
  4. //  
  5. //  Created by Rio.King on 13-9-22.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     [self createPlist];  
  21.     [self readPlist];  
  22.       
  23. }  
  24.   
  25.   
  26. -(void)readPlist{  
  27.     //搜索Document路径  
  28.     NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];  
  29.       
  30.     NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];  
  31.     NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];  
  32.     NSLog(@"%@",dict);  
  33. }  
  34.   
  35. -(void)createPlist{  
  36.     NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];  
  37.     [dict setObject:@"chaoyuan" forKey:@"name"];  
  38.     [dict setObject:[NSNumber numberWithInt:21] forKey:@"age"];  
  39.     [dict setObject:@"www.chaoyuan.sinaapp.com" forKey:@"homepage"];  
  40.       
  41.     //获取Document目录  
  42.     NSString *home = NSHomeDirectory();  
  43.       
  44.     NSString *documents = [home stringByAppendingPathComponent:@"Documents"];  
  45.     NSLog(@"%@",documents);  
  46.       
  47.     NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];  
  48.       
  49.     //写到.plist文件中去  
  50.     [dict writeToFile:path atomically:YES];  
  51. }  
  52.   
  53. @end  

注意:

  • 属性列表是一种XML格式的文件,拓展名为 plist
  • 如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等基本类型,就可以使用 writeToFile:atomically:方法直接将对象写到属性列表文件中。



数据持久化之NSKeyedArchiver


 基本的数据类型如NSString、NSDictionary、NSArray、NSData、NSNumber等可以用属性列表的方法持久化到.plist 文件中,但如果是一些自定义的类的话,属性列表的方法就不管用了。archiver 方法可以做到。


编码如下:

     首先新建一个person类,定义它的三个属性,如下:

[cpp] view plaincopyprint?
  1. //  
  2. //  person.h  
  3. //  数据持久化之archiver  
  4. //  
  5. //  Created by Rio.King on 13-9-22.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface person : UIView<NSCoding>  
  12. @property(nonatomic, assign) int age;  
  13. @property(nonatomic, copy)NSString *name;  
  14. @property(nonatomic, assign)float height;  
  15. @end  

[cpp] view plaincopyprint?
  1. //  
  2. //  person.m  
  3. //  数据持久化之archiver  
  4. //  
  5. //  Created by Rio.King on 13-9-22.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "person.h"  
  10.   
  11. @implementation person  
  12.   
  13.   
  14. #pragma mark 写入文件  
  15. -(void)encodeWithCoder:(NSCoder *)encoder{  
  16.     [super encodeWithCoder:encoder];//不要忘了这个  
  17.     [encoder encodeInt:self.age forKey:@"age"];  
  18.     [encoder encodeObject:self.name forKey:@"name"];  
  19.     [encoder encodeFloat:self.height forKey:@"height"];  
  20. }  
  21.   
  22. #pragma mark 从文件中读取  
  23. -(id)initWithCoder:(NSCoder *)decoder{  
  24.     self = [super initWithCoder:decoder];//不要忘了这个  
  25.     self.age = [decoder decodeIntForKey:@"age"];  
  26.     self.name = [decoder decodeObjectForKey:@"name"];  
  27.     self.height = [decoder decodeFloatForKey:@"height"];  
  28.       
  29.       
  30.     return self;  
  31. }  
  32.   
  33.   
  34. -(NSString *)description{  
  35.     return [NSString stringWithFormat:@"name = %@, age = %d, height = %f",self.name,self.age,self.height];  
  36. }  
  37.   
  38. //释放资源  
  39. -(void)dealloc{  
  40.     [super dealloc];  
  41.     [_name release];  
  42. }  
  43. @end  

然后再ViewController.m文件中写如下代码:

[cpp] view plaincopyprint?
  1. //  
  2. //  ViewController.m  
  3. //  数据持久化之archiver  
  4. //  
  5. //  Created by Rio.King on 13-9-22.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10. #import "person.h"  
  11.   
  12. @interface ViewController ()  
  13.   
  14. @end  
  15.   
  16. @implementation ViewController  
  17.   
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     [self createPerson];  
  22.     [self readPerson];  
  23. }  
  24.   
  25. //创建  
  26. -(void)createPerson{  
  27.       
  28.     person *p = [[[person alloc] init] autorelease];  
  29.     p.age = 20;  
  30.     p.name = @"Rio";  
  31.     p.height =1.75f;  
  32.       
  33.     //获得Document的路径  
  34.     NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];  
  35.     NSString *path = [documents stringByAppendingPathComponent:@"person.archiver"];//拓展名可以自己随便取  
  36.       
  37.     [NSKeyedArchiver archiveRootObject:p toFile:path];  
  38.       
  39. }  
  40.   
  41. //读取  
  42. -(void)readPerson{  
  43.     NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];  
  44.     NSString *path = [documents stringByAppendingPathComponent:@"person.archiver"];  
  45.     person *person1 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];  
  46.     NSLog(@"%@",person1);  
  47. }  
  48.   
  49. @end  

,,在写ViewController.m文件代码的时候,必须在头文件中遵循NSCoding协议。

[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<NSCoding>  
  4.   
  5. @end  

运行结果如下:

2013-09-22 13:31:39.509 数据持久化之archiver[1080:c07] name = Rio, age = 20, height = 1.750000


注意事项



数据持久化之preference


preference(偏好设置)是数据持久化的几个方法中最简单的一个,常用于保存少量数据

代码如下:

[cpp] view plaincopyprint?
  1. //  
  2. //  ViewController.m  
  3. //  preference(利用偏好设置保存数据)  
  4. //  
  5. //  Created by Rio.King on 13-9-22.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     [self save];  
  21.     [self read];  
  22. }  
  23.   
  24. #pragma mark 保存  
  25. -(void)save{  
  26.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
  27.     [defaults setObject:@"chaoyuan" forKey:@"username"];  
  28.     [defaults setFloat:18.6 forKey:@"text_size"];  
  29.     [defaults setBool:YES forKey:@"auto_login"];  
  30.       
  31.     //强制数据保存  
  32.     [defaults synchronize];  
  33. }  
  34.   
  35. #pragma mark 读取  
  36. -(void)read{  
  37.     NSUserDefaults *defualts = [NSUserDefaults standardUserDefaults];  
  38.     NSString *username = [defualts objectForKey:@"username"];  
  39.     float textsize = [defualts floatForKey:@"text_size"];  
  40.     BOOL autoLogin = [defualts boolForKey:@"auto_login"];  
  41.     NSLog(@"username = %@",username);  
  42.     NSLog(@"textsize = %0.1f",textsize);  
  43.     NSLog(@"autoLogin = %i",autoLogin);  
  44. }  
  45.   
  46. @end  

运行结果如下:

2013-09-22 16:30:42.183 preference(利用偏好设置保存数据)[745:c07] username = chaoyuan

2013-09-22 16:30:42.185 preference(利用偏好设置保存数据)[745:c07] textsize = 18.6

2013-09-22 16:30:42.189 preference(利用偏好设置保存数据)[745:c07] autoLogin = 1


也可以到/Users/Rio/Library/Application Support/iPhone Simulator/6.1/Applications/952F88F8-0A15-4722-903D-9D16D291CA1C/Library/Preference/  目录下查看生成的.plist文件(Rio是自己的名字,952F88F8-0A15-4722-903D-9D16D291CA1C根据个人会有所不同,一般通过生成时间就可以找到该项目),如我这边生成的文件如下:







0 0
原创粉丝点击