iOS 各种系统文件目录 临时,缓存,document,lib,归档,序列化
- /**
- 1:Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
- 2:tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
- 3:Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
- */
- NSArray *paths1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
- , NSUserDomainMask
- , YES);
- NSString *documentsDirect=[paths1 objectAtIndex:0];
- assert(1 == paths1.count);
- NSLog(@">>documentsDirect=%@",documentsDirect);
- NSArray *Librarypaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSDocumentDirectory, YES);
- NSString* libraryDirectory = [Librarypaths objectAtIndex:0];
- NSLog(@">>Librarypaths.length =%d",[Librarypaths count]);
- assert(1 < Librarypaths.count);
- NSLog(@"libraryDirectory=%@",libraryDirectory);
- //如果要指定其他文件目录,比如Caches目录,需要更换目录工厂常量,上面代码其他的可不变
- NSArray *pathcaches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory
- , NSUserDomainMask
- , YES);
- NSString* cacheDirectory = [pathcaches objectAtIndex:0];
- NSLog(@"cacheDirectory=%@",cacheDirectory);
- /**
- 使用NSSearchPathForDirectoriesInDomains只能定位Caches目录和Documents目录。
- tmp目录,不能按照上面的做法获得目录了,有个函数可以获得应用的根目录
- */
- NSString *tempDir1=NSHomeDirectory() ;
- NSString *tempDir2=NSTemporaryDirectory();
- NSLog(@"tempDir1=%@",tempDir1);
- NSLog(@"tempDir2=%@",tempDir2);
/**
1:Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
2:tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
3:Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
*/
NSArray *paths1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
, NSUserDomainMask
, YES); NSString *documentsDirect=[paths1 objectAtIndex:0];
assert(1 == paths1.count);
NSLog(@">>documentsDirect=%@",documentsDirect); NSArray *Librarypaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSDocumentDirectory, YES);
NSString* libraryDirectory = [Librarypaths objectAtIndex:0];
NSLog(@">>Librarypaths.length =%d",[Librarypaths count]);
assert(1 < Librarypaths.count); NSLog(@"libraryDirectory=%@",libraryDirectory); //如果要指定其他文件目录,比如Caches目录,需要更换目录工厂常量,上面代码其他的可不变
NSArray *pathcaches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory
, NSUserDomainMask
, YES);
NSString* cacheDirectory = [pathcaches objectAtIndex:0];
NSLog(@"cacheDirectory=%@",cacheDirectory);
/**
使用NSSearchPathForDirectoriesInDomains只能定位Caches目录和Documents目录。 tmp目录,不能按照上面的做法获得目录了,有个函数可以获得应用的根目录
*/
NSString *tempDir1=NSHomeDirectory() ;
NSString *tempDir2=NSTemporaryDirectory();
NSLog(@"tempDir1=%@",tempDir1);
NSLog(@"tempDir2=%@",tempDir2);
归档 普通自定义对象和字节流之间的转换
序列化 某些特定类型(NSDictionary, NSArray, NSString, NSDate, NSNumber,NSData)的数据和字节流之间(通常将其保存为plist文件)的转换
2.1 归档
如果我们需要将自定义的一个对象保存到文件,应该如何做呢?
这里引入两个东西:一个是NSCoding协议 ;另一个是NSKeyedArchiver,NSKeyedArchiver其实继承于NSCoder,可以以键值对的方式将对象的属性进行序列化和反序列化。
具体的过程可以这样描述 通过NSKeyedArchiver 可以将实现了NSCoding协议的对象 和 字节流 相互转换 。
像一些框架中的数据类型如NSDictionary,NSArray,NSString... 都已经实现了NSCoding协议,所以可以直接对他们进行归档操作。
这里来一个比较完整的例子,一个Address类,一个User类,User类下有个Address类型的属性
- #import <Foundation/Foundation.h>
- @interface Address : NSObject<NSCoding>
- {
- NSString *country;
- NSString *city;
- }
- @property(nonatomic,copy) NSString *country;
- @property(nonatomic,copy) NSString *city;
- @end
#import <Foundation/Foundation.h> @interface Address : NSObject<NSCoding>
{
NSString *country;
NSString *city;
}
@property(nonatomic,copy) NSString *country;
@property(nonatomic,copy) NSString *city;
@end
- #import "Address.h"
- @implementation Address
- @synthesize country;
- @synthesize city;
- - (void)encodeWithCoder:(NSCoder *)aCoder{
- [aCoder encodeObject:country forKey:@"country"];
- [aCoder encodeObject:city forKey:@"city"];
- }
- - (id)initWithCoder:(NSCoder *)aDecoder{
- if (self = [super init]) {
- [self setCountry:[aDecoder decodeObjectForKey:@"country"]];
- [self setCity:[aDecoder decodeObjectForKey:@"city"]];
- } return self;
- }
- @end
#import "Address.h" @implementation Address
@synthesize country;
@synthesize city;
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:country forKey:@"country"];
[aCoder encodeObject:city forKey:@"city"];
}
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
[self setCountry:[aDecoder decodeObjectForKey:@"country"]];
[self setCity:[aDecoder decodeObjectForKey:@"city"]];
} return self;
}
@end
- #import <Foundation/Foundation.h>
- #import "Address.h"
- @interface User : NSObject<NSCoding>{
- NSString *_name;
- NSString *_password;
- Address *_address;
- }
- @property(nonatomic,copy) NSString *name;
- @property(nonatomic,copy) NSString *password;
- @property(nonatomic,retain) Address *address;
- @end
#import <Foundation/Foundation.h>
#import "Address.h"
@interface User : NSObject<NSCoding>{
NSString *_name;
NSString *_password;
Address *_address;
}
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *password;
@property(nonatomic,retain) Address *address; @end
- #import "User.h"
- @implementation User
- @synthesize name = _name;
- @synthesize password = _password;
- @synthesize address = _address;
- - (void)encodeWithCoder:(NSCoder *)aCoder{
- [aCoder encodeObject:_name forKey:@"name"];
- [aCoder encodeObject:_password forKey:@"password"];
- [aCoder encodeObject:_address forKey:@"address"];
- }
- - (id)initWithCoder:(NSCoder *)aDecoder{
- if (self = [super init]) {
- [self setName:[aDecoder decodeObjectForKey:@"name"]];
- [self setPassword:[aDecoder decodeObjectForKey:@"password"]];
- [self setAddress:[aDecoder decodeObjectForKey:@"address"]];
- }
- return self;
- }
- @end
#import "User.h" @implementation User
@synthesize name = _name;
@synthesize password = _password;
@synthesize address = _address; - (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_password forKey:@"password"];
[aCoder encodeObject:_address forKey:@"address"];
}
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
[self setName:[aDecoder decodeObjectForKey:@"name"]];
[self setPassword:[aDecoder decodeObjectForKey:@"password"]];
[self setAddress:[aDecoder decodeObjectForKey:@"address"]];
}
return self;
} @end
操作应用
- NSString *tempDir2=NSTemporaryDirectory();
- // Do any additional setup after loading the view, typically from a nib.
- Address *myAddress = [[Address alloc] init] ;
- myAddress.country = @"中国";
- myAddress.city = @"杭州";
- User *user = [[User alloc] init] ;
- user.name = @"卢克";
- user.password = @"lukejin";
- user.address = myAddress;
- //归档 保存的是plist的二进制数据格式
- NSString *path = [tempDir2 stringByAppendingPathComponent:@"user"];
- [NSKeyedArchiver archiveRootObject:user toFile:path];
- //从文档中读取
- User *object = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
- NSLog(@"object.name : %@",object.name);
NSString *tempDir2=NSTemporaryDirectory(); // Do any additional setup after loading the view, typically from a nib.
Address *myAddress = [[Address alloc] init] ;
myAddress.country = @"中国";
myAddress.city = @"杭州";
User *user = [[User alloc] init] ; user.name = @"卢克";
user.password = @"lukejin";
user.address = myAddress;
//归档 保存的是plist的二进制数据格式
NSString *path = [tempDir2 stringByAppendingPathComponent:@"user"];
[NSKeyedArchiver archiveRootObject:user toFile:path]; //从文档中读取
User *object = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"object.name : %@",object.name);
使用数据对象自带的方法,如字典类写文件:
数据:
- NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] init] ;
- [dataDictionary setValue:[NSNumber numberWithInt:222] forKey:@"intNumber"];
- [dataDictionary setValue:[NSArray arrayWithObjects:@"1",@"2", nil] forKey:@"testArray"];
NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] init] ;
[dataDictionary setValue:[NSNumber numberWithInt:222] forKey:@"intNumber"];
[dataDictionary setValue:[NSArray arrayWithObjects:@"1",@"2", nil] forKey:@"testArray"];
写文件
- [dataDictionary writeToFile:@"/Users/zhoumoban/Desktop/test.plist" atomically:YES];
[dataDictionary writeToFile:@"/Users/zhoumoban/Desktop/test.plist" atomically:YES];
读文件:
- NSDictionary *dictionaryFromFile = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhoumoban/Desktop/test.plist"];
- NSLog(@"%@",[dictionaryFromFile objectForKey:@"intNumber"]);
NSDictionary *dictionaryFromFile = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhoumoban/Desktop/test.plist"];
NSLog(@"%@",[dictionaryFromFile objectForKey:@"intNumber"]);
另外:使用NSPropertyListSerialization类。通过NSPropertyListSerialization类可以将数据对象直接转成NSData或者直接写到文件或者流中去
- NSString *error;
- NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:dataDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
- if(xmlData) {
- NSLog(@"No error creating XML data.");
- [xmlData writeToFile:@"/Users/zhoumoban/Desktop/test2.plist" atomically:YES];
- } else {
- if (error) {
- NSLog(@"error:%@", error);
- // [error release];
- }
- }
- //读取
- NSDictionary *dictionaryFromFile2 = (NSDictionary *)[NSPropertyListSerialization propertyListWithData:[NSData dataWithContentsOfFile:@"/Users/zhoumoban/Desktop/test2.plist"] options:0 format:NULL error:&error];
- NSLog(@"===%@",[dictionaryFromFile2 objectForKey:@"intNumber"]);
iOS 各种系统文件目录 临时,缓存,document,lib,归档,序列化的更多相关文章
- iOS开发网络篇—数据缓存
iOS开发网络篇—数据缓存 一.关于同一个URL的多次请求 有时候,对同一个URL请求多次,返回的数据可能都是一样的,比如服务器上的某张图片,无论下载多少次,返回的数据都是一样的. 上面的情况会造 ...
- (转)苹果iOS开发者账号过期临时解决方法
苹果iOS开发者账号过期临时解决办法 苹果iOS开发者账号一年的费用是99美金,作者最近由于各种原因,导致renew没能在账号过期之前支付好,所以在账号过期等待renew的期间,试了试一些非正常手段, ...
- 微信公众号弹出框在IOS最新系统中点击键盘上的“完成”导致事件无法触发问题
微信公众号弹出框在IOS最新系统中点击键盘上的"完成"导致事件无法触发问题 问题描述 微信公众号中有项功能是弹框模态框,输入信息后保存操作.但是在IOS系统中发现,当输入内容后,点 ...
- Linux常用系统文件目录结构
Linux常用系统文件目录结构 bin:全称binary,含义是二进制.该目录中存储的都是一些二进制文件,文件都是可以被运行的. dev:该目录主要存放的是外接设备,例如硬盘.其他的光盘等.在其中的外 ...
- Windows系统下Memcached缓存系列二:CouchbaseClient(c#客户端)的详细试用,单例模式
在上一篇文章里面 ( Windows系统下Memcached缓存系列一:Couchbase(服务器端)和CouchbaseClient(c#客户端)的安装教程 ),我们介绍了服务器端的安装和客户端的安 ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
- Ubuntu等Linux系统清除DNS缓存的方法
buntu等Linux系统清除DNS缓存的方法 直接说方法: 如果系统下有nscd,那么就直接 sudo /etc/init.d/nscd restart 如果没有也没关系,网上接受的方法大都是 su ...
- iOS 捕获系统外异常
iOS 捕获系统外异常 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太 ...
- 好系统重装助手教你清理win7系统中DNS缓存
在我们使用电脑的过程中,有时候一个经常用的网页突然打不开了,遇到这种情况,清理一下DNS缓存就可以解决了.如何清理DNS缓存?小编这就给大家说一种最简单的方法. 1.组合键:win+R,输入cmd,点 ...
随机推荐
- 学习总结 java 创建及其练习
创建: 打开eclipse—文件—新建—java项目—项目名称命名—点击texe-1练习下拉箭头—右击src—新建—类—设置类名称(名称设置时不要添加空格),在“想要创建哪些方法跟”下面点击:publ ...
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
消息队列处理后台任务带来的问题 项目中经常会有后台运行任务的需求,比如发送邮件时,因为要连接邮件服务器,往往需要5-10秒甚至更长时间,如果能先给用户一个成功的提示信息,然后在后台慢慢处理发送邮件的操 ...
- Android IOS WebRTC 音视频开发总结(五五)-- 音视频通讯中的抗丢包与带宽自适应原理
本文主要分析webrtc中的抗丢包与带宽自适应原理,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help 文章内容主要来自中国电信北京研究院丁博士在 ...
- iConvert Icons 图标转换生成利器,支持Windows, Mac OS X, Linux, iOS,和Android等系统
这是一款在线图标转换工具,生成的图标支持Windows, Mac OS X, Linux, iOS, 和 Android等主流系统. 可以上传图标文件转化成另一个平台下的图标文件,例如将windows ...
- POJ C程序设计进阶 编程题#3 : 排队游戏
编程题#3:排队游戏 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 在幼儿 ...
- 设置field的背景颜色以及对stylesheet的理解
今天遇到一个需求:在做页面输入验证的时候,如果用户没有输入某个项,那么这个项显示为红色,一直没头绪,也找peoplebook,发现field有一个style的方法,后来又在谷歌上找,终于找到了方法: ...
- C#winform设置DateTimePicker的时间格式
在对DateTimePicker进行时间格式设置时候,要先对属性Format设置为"Custom"自定义格式,然后再CustomFormat里面进行格式设置 比如"yyy ...
- css半透明
filter:alpha(opacity=80); /*支持 IE 浏览器*/-moz-opacity:0.80; /*支持 FireFox 浏览器*/opacity:0.80; /*支持 Chrom ...
- redis的数据类型
redis有string,hash,list,sets.zsets几种数据类型 1.string数据类型 可包含任何数据,是二进制安全的,比如图片或者序列化的对象set key valueset na ...
- mysql查询语句(mysql学习笔记七)
Sql语句 一般顺序GHOL : group by,having ,order by,limit 如果是分组,应该使用对分组字段进行排序的group by语法 ...