iphone 数据存储之属性和归档archive

摘要:
在IPHONE中永久存储数据有四种方法:属性列表、存档、SQLITE3和核心数据。此外,IPHONE的每个应用程序都有三个文件夹:文档、tmp和库,分别称为应用程序数据存储、临时数据存储和数据库存储。由于时间限制,我花了时间完成了这篇文章。

     在IPHONE中有四种方式可以永久存储数据分别是属性列表、归档、SQLITE3、coredata。前两者、后二者操作的时候有些地方是相同的,以属 性列表和归档来说都会用writeToFile/URL:path atomically:flag 和initWithContentofFile/URL:path;两都都不能直接操作基本数据类型,不过前者不能操作自定义的类,而后者可以通过实现 NSCoding协议来达到目的。另外要说点的就是IPHONE每个应用都会有三个文件夹分别是documents、tmp、library分别称为存储 应用的数据,临时数据,数据库。我们要保存的数据会在documents中。由于时间关系抽空再把这个文字写完整些。

#import "dataprocessAppDelegate.h"
@implementation dataprocessAppDelegate
@synthesize window;
@synthesize dataArray;
-(NSString*)pathFileForProcess:(NSString *)pathName{
NSArray *directory=NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
return [[directory objectAtIndex:0] stringByAppendingPathComponent:pathName];
}
z
-(void)writeDataToFile{
firstData = [[NSString alloc] initWithString:@"im first!"];
secondData = [[NSString alloc] initWithString:@"im secondData!"];
thirdData = [[NSString alloc] initWithString:@"im thirdData!"];
NSLog(@"write:\n first: %@ \nscond: %@ \nthird: %@", firstData, secondData, thirdData);
NSMutableArray *tmp = [[NSMutableArray alloc] init];
[tmp addObject:firstData];
[tmp addObject:secondData];
[tmp addObject:thirdData];
self.dataArray = tmp;
[tmp release];

[firstData release];[secondData release];[thirdData release];
BOOL bWrite = [dataArray writeToFile:[self pathFileForProcess:@"myTest.txt"] atomically:YES];
}//属性读
-(void)readDataFromFile{
if([[NSFileManager defaultManager] fileExistsAtPath:[self pathFileForProcess:@"myTest.txt"]]){
      NSMutableArray  *tmpRead = [[NSMutableArray alloc] initWithContentsOfFile:[self pathFileForProcess:@"myTest.txt"]];
  self.dataArray = tmpRead;
      [tmpRead release];

    firstData = [dataArray objectAtIndex:0];
    secondData = [dataArray objectAtIndex:1];
    thirdData = [dataArray objectAtIndex:2];

    NSLog(@"read:\n first: %@ \nscond: %@ \nthird: %@", firstData, secondData, thirdData);
return;
}
NSLog(@"PROCESS FIRLE DOESNT EXITS!");
}

#pragma mark -------object-------------
//归档写
-(void)processObjectWrite{
person *pObject= [[person alloc] init];
pObject.name = [[NSString alloc] initWithString:@"wenQiang"];
pObject.profession = [[NSString alloc] initWithString:@"project manager"];
//[pObject setAge:24 andMarry: NO];

//NSMutableArray *testData = [[NSMutableArray alloc] init];
NSMutableData *data=[[NSMutableData alloc] init];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:pObject forKey:@"kObject"];
[archiver finishEncoding];
Boolean bWrite = [data writeToFile:[self pathFileForProcess:@"object2.txt"] atomically:YES];
if(bWrite) NSLog(@"ok..."); else NSLog(@"write error!");
[archiver release];
//[pObject release];
}、、归档读
-(void)processObjectRead{
NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:[self pathFileForProcess:@"object2.txt"]];
NSLog(@"data %@..", data);

NSKeyedUnarchiver *unchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
person *tmp = [unchiver decodeObjectForKey:@"kObject"];
[unchiver finishDecoding];

NSLog(@"OBJECT: name: %@ profession: %@\nage: %@\n marry:%@", tmp.name, tmp.profession);
[unchiver release];
//[tmp release];

//实现
- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
//[self writeDataToFile];
//[self readDataFromFile];

[self processObjectWrite];
[self processObjectRead];
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
[dataArray release];
    [super dealloc];
}


@end
//以下是自定义的类
#pragma mark-----------------------class person----------------
#define       kName              @"keyName"
#define       kProfession @"keyProfession"
#define       kAge @"keyAge"
#define       kMarry @"keyMarry"

@implementation person
@synthesize name;
@synthesize profession;
#pragma mark---------------nscoding delegate 2 method---------
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:name forKey: kName];
[aCoder encodeObject:profession forKey: kProfession];
// [aCoder encodeObject:Age forKey: kAge];
// [aCoder encodeObject:marry forKey:kMarry];
}
- (id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init]){
self.name = [aDecoder decodeObjectForKey:kName];
self.profession = [aDecoder decodeObjectForKey:kProfession];
//Age = [aDecoder decodeObjectForKey:kAge];
// marry=[aDecoder decodeObjectForKey:kMarry];
}
return self;
}
#pragma mark ---------------NSCopying 1 method-------------
- (id)copyWithZone:(NSZone *)zone{
person *tmp = [[[self class] allocWithZone:zone] init];

tmp.name = [self.name copy];
tmp.profession = [self.profession copy];
return nil;
}

-(void)dealloc{
[name release];
[profession release];
[super dealloc];
}
//-(void)setAge:(NSInteger)age andMarry:(Boolean)b{
// Age = age;
// marry = b;
//}

@end

免责声明:文章转载自《iphone 数据存储之属性和归档archive》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Git的常用命令SourceMonitor安装及使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

NSBundle的使用,注意mainBundle和Custom Bundle的区别

1、[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器 Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样: NSString *earth = [[NSBundle mainBundle] pathForResource:@"Brad Cox" ofType:@"p...

ios开发 静态库制作

IOS程序是由Objective-c语言构成,而是Objective-c语言中每一个类又分成 .h .m 文件。静态库可以将这些程序的类封装成一个.a文件,第三方应用程序只需要拿到这个.a文件和代码对应的.h文件即可使用静态库中封装的方法。总的来说IOS静态库适合去制作第三方提供的SDK,废话不多说了我们直接进正题。 制作静态库 代码如下 1 #i...

iOS开发时间戳与时间,时区的转换,汉字与UTF8,16进制的转换

原文:http://blog.sina.com.cn/s/blog_68661bd80101njdo.html //获取当前系统的时间戳+(long)getTimeSp{    long time;    NSDate *fromdate=[NSDate date];    time=(long)[fromdate timeIntervalSince197...

前端实现大文件上传分片上传断点续传

总结一下大文件分片上传和断点续传的问题。因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况。http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议自动会将文件切片(分块),但这不是我们现在说的重点,我们要做的事是保证在网络中断后1G的文件已上传的那部分在下次网络连接时不必再重传。所以我们本地在上传的时候,要将大文件...

iphone手机与PC蓝牙出现感叹号且无法修复解决方案

解决方案如下: 1.需要下载Windows Mobile 6.5 的驱动 drvupdate-amd64.exe ,下载需要正版验证,手动安装驱动,具体步骤Google 2. 如果在BlueTooth Device 下有Wireless 的服务关掉,具体步骤Google 完成以上2步就可以通过PC e.g. Windows Media 播放Iphone的音...

iOS开发中WiFi相关功能总结

http://www.cocoachina.com/ios/20160715/17022.html 投稿文章,作者:Haley_Wong(简书) 查漏补缺集是自己曾经做过相关的功能,但是重做相关功能或者重新看到相关功能的实现,感觉理解上更深刻。这一类的文章集中记录在查漏补缺集。 iOS 开发中难免会遇到很多与网络方面的判断,这里做个汇总,大多可能是与WiF...