UI第六章持久化和网络请求

本文介绍iOS开发中的沙盒概念及如何通过不同目录管理应用数据,包括Documents、Library和tmp目录的用途,同时讲解了归档作为数据持久化的一种方法。

1、沙盒:把每个应用分配到一个独立的文件夹,并且将应用程序所能读写的位置几乎限定在这个文件夹里,这个文件         夹可以看做特殊的区域,我们称这应用为沙盒。

持久化就是将那些临时数据(程序退出便丢失)进行存储,让其变为非易失的数据,之前的NSUserDefaults也是持久化的一种。

2、每个应用程序都有四部分:

 1、应用程序Bundle:(只能读、不能修改)包含了应用程序所有用到的资源(图像,声音,代码,xib文件),在编码过程中使用NSBundle来进行访问。  

NSBundle *bundle=[NSBundle mainBundle];

    NSString *filePath=[bundle pathForResource:@"fileName" ofType:@"extName"];


 2、Documents目录:应用程序将其数据储存在Documents目录中。

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);

    NSString *document=[paths objectAtIndex:0];


 3、Library目录:用户首选项设置都在储存中Library/Preferences文件夹中。

 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);

    NSString *library=[paths objectAtIndex:0];

    NSString *fileName=[library stringByAppendingPathComponent:@"fileName.extName"];



 4、tmp目录:tmp目录供应应用程序储存临时文件,应用程序中产生的文档也可以临时性的放入此目录,但是此目录在iTunes同步设备室并不备份。当然应用程序中产生的临时文件,还需要应用程序来负责删除这些不用的临时文件。

NSString *tmp=NSTemporaryDirectory();

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

 

    //归档,持久化的一种。他能对一个实现了NSCoding协议的实例对象归档,只能一个一个的对象归档

    //对存储了字符串的数组进行归档NSStringNSNumber都实现了NSCoding协议

    NSArray* array=[NSArray arrayWithObjects:@"Moves Likes Jagger",@"Payphone",@"She Will be loved", nil];

    NSMutableData* data=[[NSMutableData alloc]initWithCapacity:100];

    //定义一个归档器

    NSKeyedArchiver* archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

    [archiver encodeObject:array forKey:@"arrayKey"];

    //把归档后的数据写入文件保存

    [data writeToFile:@"文件路径" atomically:YES];

    [archiver finishEncoding];

    [archiver release];

    [data release];


    //获取归档数据

    NSData* data1=[[NSData alloc]initWithContentsOfFile:@"文件路径"];

    //定义一个解档器

    NSKeyedUnarchiver* unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data1];

    //按关键字获取对应的数据

    NSArray* array1=[unarchiver decodeObjectForKey:@"arrayKey"];

    [unarchiver finishDecoding];

    [unarchiver release];

    [data1 release];

    

    //网络连接有三种方式同步,异步(代码块和协议)

    //同步连接

    NSString* path=@"http://www.baidu.com/img/bdlogo.gif";

    NSURL* url=[[NSURL alloc]initFileURLWithPath:path];

    NSURLRequest* request=[[NSURLRequest alloc]initWithURL:url];

    NSURLResponse* response=nil;

    NSError* error=nil;

    NSData* receiveData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    UIImage* image=[UIImage imageWithData:receiveData];

    //异步代码块连接

    NSString* path1=@"http://www.baidu.com/img/bdlogo.gif";

    NSURL* url1=[[NSURL alloc]initFileURLWithPath:path1];

    NSURLRequest* request1=[[NSURLRequest alloc]initWithURL:url1];

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

        UIImage* image=[UIImage imageWithData:data];

    }];

    //异步协议 需要遵循NSURLConnectionDataDelegate协议

}


//实现NSURLConnectionDataDelegate协议方法

//只调用一次,获取服务器响应后调用

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{


}

//调用多次,每次接受数据块的时候调用

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{


}

//整个连接请求完成,数据加载完毕之后调用方法

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{


}

JSON是一种轻量级的数据交换格式。把二进制数据转换成JSON形式的有利于在网上传播。我们要做的就是把JSON格式的二进制数据转换成我们看得懂的数据。

//JSON解析

    //解析的文件所在路径

    NSString* path=[[NSBundle mainBundle]pathForResource:@"person" ofType:@"json"];

    //把内容转换成二进制数据

    NSData* data=[NSData dataWithContentsOfFile:path];

    //实行解析

    NSDictionary* dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    NSLog(@"%@",dict);

XML是可扩展标记语言,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。它非常适合Web传输。

NSString* path=[[NSBundle mainBundle]pathForResource:@"users" ofType:@"xml"];

    NSData* data=[NSData dataWithContentsOfFile:path];

    GDataXMLDocument* doc=[[GDataXMLDocument alloc]initWithData:data options:kNilOptions error:nil];

    //获取根元素

    GDataXMLElement* root=[doc rootElement];

    //获取根元素下关键字为User的元素

    GDataXMLElement* user=[[root elementsForName:@"User"]objectAtIndex:0];

    //获取元素的属性

    NSString* userID=[[user attributeForName:@"id"]stringValue];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值