高级UIKit-04(NSUserDefaults、NSKeyedArchiver、对象归档方法) - 回读(IOS)

来源:互联网 发布:php assumed 编辑:程序博客网 时间:2024/05/25 23:25
iOS中的数据持久化方式,基本上有以下四种:


 属性列表、对象归档、SQLite3和Core Data


1.属性列表
涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults​ standardUserDefaults​]就够用了

@interface User : NSObject <NSCoding>
@property (nonatomic, assign) NSInteger userID;
@property (nonatomic, copy) NSString *name;
@end

使用方法
1).分开存取
// 存
[[NSUserDefaults standardUserDefaults] setInteger:userID forKey:@”userID”];
​[[NSUserDefaults standardUserDefaults] setObject:name forKey:@”name”];
// 取
NSInteger uId = [[[NSUserDefaults standardUserDefaults] integerValueForKey:@”userID”];
NSString* name = [[NSUserDefaults standardUserDefaults] stringForKey:@”name”];​
2).按对象存取
// 存
[[NSUserDefaults standardUserDefaults] setObject:self forKey:@”user”];
// 取
User* u = [[NSUserDefaults standardUserDefaults] objectForKey”@”user”];

2.对象归档
要使用对象归档,对象必须实现NSCoding协议.大部分Object C对象都符合NSCoding协议,也可以在自定义对象中实现NSCoding协议,要实现NSCoding协议,实现两个方法:
- (void) encodeWithCoder:(NSCoder *)encoder 与 -(void)initWithCoder:(NSCoder *)encoder
同时,建议对象也同时实现NSCopying协议,该协议允许复制对象,要实现NSCopying协议须实现 -(id)copyWithZone:(NSZone *)zone 方法 。
@interface User : NSObject <NSCoding>
@property (nonatomic, assign) NSInteger userID;
@property (nonatomic, copy) NSString *name;
​@end

@implementation User
// 以下两个方法一定要实现,不然在调用的时候会crash
- (void)encodeWithCoder:(NSCoder *)aCoder; 
{
// 这里放置需要持久化的属性
[aCoder encodeObject:[NSNumber numberWithInteger:self.userID] forKey:@”userID”];
[aCoder encodeObject:self.name forKey:@"name"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [self init])
{
//  这里务必和encodeWithCoder方法里面的内容一致,不然会读不到数据
self.userID = [[aDecoder decodeObjectForKey:@"userID"] integerValue];
self.name = [aDecoder decodeObjectForKey:@"name"];
}
return self;
}

// 使用方法
+ (BOOL)save {
NSError *error = nil;
// 确定存储路径,一般是Document目录下的文件
NSString* fileName = [self getFileName];
NSString* filePath = [self getFilePath];
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@”创建用户文件目录失败”);
return NO;
}
return [NSKeyedArchiver archiveRootObject:self toFile:[fileName:userId]];
}
​@end

3.SQLite3​
SQLite是一个开源的嵌入式关系数据库,它在2000年由D. Richard Hipp发布,它的减少应用程序管理数据的开销,SQLite可移植性好,很容易使用,很小,高效而且可靠。
SQLite嵌入到使用它的应用程序中,它们共用相同的进程空间,而不是单独的一个进程。从外部看,它并不像一个RDBMS,但在进程内部,它却是完整的,自包含的数据库引擎。 嵌入式数据库的一大好处就是在你的程序内部不需要网络配置,也不需要管理。因为客户端和服务器在同一进程空间运行。SQLite 的数据库权限只依赖于文件系统,没有用户帐户的概念。SQLite 有数据库级锁定,没有网络服务器。它需要的内存,其它开销很小,适合用于嵌入式设备。你需要做的仅仅是把它正确的编译到你的程序。
关于SQLite的开发资料较多,这里不再细说。只是建议不直接操作SQLite库,而是采用一些开源的第三方库来进行操作。比如:
FMDB:https://github.com/ccgus/fmdb.git
对SQLite都做了不错的封装。

4.​Core Data​
Core Data本质上是使用SQLite保存数据,但是它不需要编写任何SQL语句。
要使用Core Data,需要在Xcode中的数据模型编辑器中设计好各个实体以及定义好他们的属性和关系。之后,通过操作这些对象,结合Core Data完成数据的持久化:
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;
NSString *fieldName = [NSString stringWithFormat:@"test%d", i];
UITextField *theField = [self valueForKey:fieldName];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//创 建描述语句,需求Line对象。类似于在数据库中限定为Line表。​
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line"  inManagedObjectContext:context];
[request setEntity:entityDescription];
//创建限制性语句,类似于SQL语句中的 where lineNum = i​
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i];
[request setPredicate:pred];
NSManagedObject *theLine = nil;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects == nil){
NSLog(@”There was an error!”);
// Do whatever error handling is appropriate
}
if ([objects count] > 0){    //如果符合条件的object存在,则取出
theLine = [objects objectAtIndex:0];
}
else {  //如果不存在,则插入一个新的.
theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line"
inManagedObjectContext:context];
[theLine setValue:[NSNumber numberWithInt:i] forKey:@”lineNum”];  //设置这个object的属性,coredata会自动将其写入sqlite
[theLine setValue:theField.text forKey:@"lineText"];
[request release];

}
下面是其取数据的过程。


Core_Data_PersistenceAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects == nil)
{
NSLog(@”There was an error!”);
// Do whatever error handling is appropriate
}
//每一个对象在CoreData中都表示为一个NSManagedObject对象(类似于数据库表中的每一行),他的属性通过键/值 方式获取​
for (NSManagedObject *oneObject in objects)
{
NSNumber *lineNum = [oneObject valueForKey:@"lineNum"];
NSString *lineText = [oneObject valueForKey:@"lineText"];
}

[request release];​


【day05_1_UserDefault】:判断应用程序是否是第一次运行

NSUserDefaults:用来保存应用程序的配置信息如:程序运行次数,用户登陆信息等。

// 使用系统提供的NSUserDefaults对象判断    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];    int runCount = [[userDefaults objectForKey:@"runCount"] intValue];    if (runCount == 0) {        NSLog(@"第一次运行!!!");    }    runCount++;    [userDefaults setObject:[NSNumber numberWithInt:runCount] forKey:@"runCount"];    [userDefaults synchronize]; // 同步

练习:三个界面ABC,运行次数奇数次进C,偶数次进B

在MXAppDelegate.m中写

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // 因为那个箭头可以直接确定谁是rootViewController,所以可以简化代码    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];    int runCount = [[userDefaults objectForKey:@"runCount"] intValue];    if (runCount % 2 == 0) {        UIViewController *bVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"bVC"];        [self.window.rootViewController presentViewController:bVC animated:YES completion:nil];        self.window.rootViewController = bVC;    }    runCount++;    [userDefaults setObject:[NSNumber numberWithInt:runCount] forKey:@"runCount"];    [userDefaults synchronize]; // 同步    return YES;}

【day05_2_ArrayArchiver】:数组归档的使用

归档的使用:把对象转成二进制NSData,可以进行保存或网络传输

如何实现归档:需要让对象实现NSCoding协议

已经实现了NSCoding协议的对象有:数组和字典

数组归档:

// 数组归档    NSArray *namesArray = @[@"张三",@"李四",@"王五"];    // 1.创建可变的data对象,装数据    NSMutableData *data = [NSMutableData data];    // 2.创建归档对象    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];    // 3.把对象编码    [archiver encodeObject:namesArray forKey:@"names"];    // 4.编码完成    [archiver finishEncoding];    // 5.保存归档    [data writeToFile:@"/Users/tarena/Desktop/namesArchiver" atomically:YES];

数组反归档:

// 数组反归档    // 1.得到data    NSData *data = [NSData dataWithContentsOfFile:@"/Users/tarena/Desktop/namesArchiver"];    // 2.创建反归档对象    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];    // 3.解码并存到数组中    NSArray *namesArray = [unArchiver decodeObjectForKey:@"names"];    // 4.循环打印    for (NSString *name in namesArray) {        NSLog(@"%@",name);    }

【day05_3_DictionaryArchiver】字典的归档反归档和数组一样。

【day05_4_AddStringToArchiver】:添加输入框内容到textView中,关闭程序后textView中内容还在,(让UITextView不可编辑,勾掉Editable)

这个使用到归档,代码:

// 加载数组归档数据-(void)loadInfo{    // 从归档中取数据    // 数组反归档    self.namesTextView.text = @"";    NSData *nameData = [NSData dataWithContentsOfFile:@"/Users/tarena/Desktop/nameArrayArchiver"]; // 得到data    NSKeyedUnarchiver *unarc = [[NSKeyedUnarchiver alloc] initForReadingWithData:nameData]; // 创建反归档对象    self.nameArray = [unarc decodeObjectForKey:@"names"]; // 解码    for (NSString *name in self.nameArray) { // 取出数组中数据拼接到textView        self.namesTextView.text = [self.namesTextView.text stringByAppendingFormat:@"%@\n",name];    }}// 添加姓名- (IBAction)addNameToTextView:(id)sender {    NSString *name = self.nameTextField.text; // 取出输入的字符串    NSMutableArray *mutableNameArray = [NSMutableArray arrayWithArray:self.nameArray]; // 把不可变数组变为可变数组    [mutableNameArray addObject:name]; // 添加字符串到数组中       // 数组归档    NSMutableData *nameData = [NSMutableData data]; // 创建可变data对象    NSKeyedArchiver *arc = [[NSKeyedArchiver alloc] initForWritingWithMutableData:nameData]; // 创建归档对象    [arc encodeObject:mutableNameArray forKey:@"names"]; // 给数组编码    [arc finishEncoding]; // 编码完成    [nameData writeToFile:@"/Users/tarena/Desktop/nameArrayArchiver" atomically:YES]; // 保存到指定的路径    [self loadInfo];}

总结:反归档如果返回数组,一般用NSArray接收,如果在程序中要改变NSArray的值,要先把它转成可变数组,然后把这个新数组重新归档。

注意归档时,要创建可变的NSData对象。

【day05_5_ArchiverPerson】:对象可以归档反归档必须做的事

1.遵守NSCoding协议

2.实现该协议的两个方法

代码如下:

// 属性编码- (void)encodeWithCoder:(NSCoder *)aCoder{       [aCoder encodeObject:self.name forKey:@"name"];       [aCoder encodeInteger:self.age forKey:@"age"];   }// 属性解码- (id)initWithCoder:(NSCoder *)aDecoder{       self = [super init];    if (self) {        self.name = [aDecoder decodeObjectForKey:@"name"];        self.age = [aDecoder decodeIntegerForKey:@"age"];    }    return self;}

【day05_6_PersonsArchiver】:人员名单,功能有添加人员,删除人员,修改人员。使用归档。

添加:

添加时要跳转到新的界面,一般这么做:

1.如果第一个界面用navigation包含了就把整个界面push到第二个界面,使用viewController连接。

2.然后设置该连线也就是segue的identifier值。

3.然后第一个界面中的控件或是tableView的cell通过一个方法[self performSegueWithIdentifier:@"personInfoVC" sender:person];进行segue跳转并可以传值。

4.在segue跳转前还有一个方法可以把传的值赋给要跳转的界面,就是- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

在添加界面中首先把归档的数据取出来添加进去,然后把新的数据重新归档。

添加代码如下:

// 添加数据- (IBAction)addOrEditButtonAction:(id)sender {// 添加        MXPerson *person = [[MXPerson alloc] init];        person.name = self.nameTextField.text;        person.age = [self.ageTextField.text intValue];               // 首先需要取出归档数据,在往里添加        NSData *data = [NSData dataWithContentsOfFile:@"/Users/tarena/Desktop/persons"];        NSKeyedUnarchiver *unarc = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];        NSArray *oldPersonArray = [unarc decodeObjectForKey:@"persons"];        NSMutableArray *newPersonArray = [NSMutableArray arrayWithArray:oldPersonArray];        [newPersonArray addObject:person]; // 添加数据               // 把对象归档        NSMutableData *personData = [NSMutableData data];        NSKeyedArchiver *arc = [[NSKeyedArchiver alloc] initForWritingWithMutableData:personData];        [arc encodeObject:newPersonArray forKey:@"persons"];        [arc finishEncoding];        [personData writeToFile:@"/Users/tarena/Desktop/persons" atomically:YES];    [self.navigationController popViewControllerAnimated:YES];}

修改:

点击cell后跳到修改界面,然后点击按钮修改。

在跳转时要把当前cell(也就是数组中的某个元素)的数据传到修改界面并显示,修改数据后重新归档。

因为在修改界面要用到第一个界面中数组数据,所以在跳转到修改界面前把自己赋给修改界面的delegate(该代理的类型就是第一个界面),然后进行修改并重新归档。

注意:在跳转时会有多个sender值,在跳转界面要做判断。本例中点击添加,传的sender是nil,点击cell传的sender就是该cell数据。

代码如下:

// 点击cell后跳转到修改界面并传cell的数据-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    MXPerson *person = self.personArray[indexPath.row];       [self performSegueWithIdentifier:@"personInfoVC" sender:person];   }// segue跳转前把数据赋给修改界面- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{       MXViewController *personInfoVC = segue.destinationViewController;    personInfoVC.person = sender;    personInfoVC.delegate = self;   }// 添加或修改数据- (IBAction)addOrEditButtonAction:(id)sender {    if (self.person) { // 修改        self.person.name = self.nameTextField.text;        self.person.age = [self.ageTextField.text intValue];        [self.delegate savePerson];    } else{ // 添加              }[self.navigationController popViewControllerAnimated:YES];}

删除:

从归档中删除,然后保存归档

代码如下:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        [self.personArray removeObjectAtIndex:indexPath.row]; // 从归档中删除        [self savePerson]; // 保存归档        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];    }      else if (editingStyle == UITableViewCellEditingStyleInsert) {        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view    }  }

0 0
原创粉丝点击