1.沙盒机制
文件夹之间不能通信
Finder前往 按Alt键 出来资源库
//返回类型是个数组 第一个参数是枚举,有很多文件夹类型
第三个参数YES的话是详细路径,NO的话~/最后一个文件夹名
//数组里放的是 同一文件下所有相同名字的文件夹 按角标取出我们要的
NSArray *arr = NSSearchPathForDirectoriesInDomains(<#NSSearchPathDirectory directory#>, <#NSSearchPathDomainMask domainMask#>, <#BOOL expandTilde#>)
//返回bundle(.app)文件夹路径
-(NSString *)searchInBundle
{
NSString *bundlePath =
[[NSBundle mainBundle]pathForResource:@"InfoPlist"
ofType:@"strings"];
return bundlePath;
}
Bundle
程序运行时不可改 安装程序是只有这里有东西 用的时候把Bundle的东西拷贝到下面三个文件夹里用
Documents文件夹 存数据库 / coreData文件 限制不能超过20M
Library文件夹 caches存储图片 网址... Preferences存储系统的配置文件NSUserDefaulfs
tmp文件夹 存储临时文件 老版本按home键自动清空 现在需要手动和上两个一样
2.简单对象的读写文件(NSString,Array,dic,set)
atomically 是否自动存储 encoding 编码格式(国际编码汉字一般用的 NSUTF8StringEncoding) error:存储错误信息双指针类型
#pragma mark 字符串的写入
-(void)writeWithString
{
NSString *str = [self
searchPath];
//添加字符
// NSString
*newPath = [str stringByAppendingString:@"/text.txt"];
//拼接字符
NSString *newPath = [NSString
stringWithFormat:@"%@/%@",str,@"text.txt"];
//
NSLog(@"newPath == %@",newPath);
NSString *writeStr =
@"xiaozhangdezifuchuan";
NSError
*error = nil;
[writeStr
writeToFile:newPath atomically:YES encoding:NSUTF8StringEncoding
error:&error];//第四个参数直接写nil也可以
if (error)
{
NSLog(@"error ==
%@",error);
}
}
#pragma mark 字符串的读取
-(void)readWithString
{
NSString *path = [self searchPath];
NSString *newPath =
[path stringByAppendingString:@"/text.txt"];
NSError *error = nil;
NSString *readStr = [NSString stringWithContentsOfFile:newPath
encoding:NSUTF8StringEncoding error:&error];//+方法
if
(error) {
NSLog(@"error =====
%@",error);
}
NSLog(@"read ==
%@",readStr);
NSString *a =
[[NSString alloc]initWithContentsOfFile:newPath encoding:NSUTF8StringEncoding
error:&error];//-方法
NSLog(@"a ===
%@",a);
}
//写入
1.创建要写入的内容
2.找路径 加新路径
3.写入方法writeToFile
4.调用
//读取
1.找路径 加名
2.读取方法ContentsOfFile
3.调用
//UIImage 转换成data类型
UIImage *image = [UIImage imageNamed:@"刘健.jpg"] ;
NSData *data =
UIImagePNGRepresentation(image);
NSString *name = @"shengqingjun";
//字符串转换成2进制
NSData *data = [name
dataUsingEncoding:NSUTF8StringEncoding];
//2进制转换成字符串 只有一个方法
NSString *str = [[NSString
alloc]initWithData:data encoding:NSUTF8StringEncoding];
3.复杂对象的读写文件(自定义model,有属性/在Foundation框架内不存在,无法通过writeToFile写入,至少有一个实例对象)
NSCoding协议
ABPersonInfo.m//自定义的类型
//归档的方法
- (void)encodeWithCoder:(NSCoder
*)aCoder
{
[aCoder encodeObject:self.name
forKey:@"name"];
[aCoder encodeObject:self.age
forKey:@"age"];
[aCoder encodeObject:self.frame
forKey:@"frame"];
}
//解档的方法
- (id)initWithCoder:(NSCoder
*)aDecoder
{
if (self = [super init])
{
self.name = [aDecoder
decodeObjectForKey:@"name"];
self.age = [aDecoder
decodeObjectForKey:@"age"];
self.frame = [aDecoder decodeObjectForKey:@"frame"];
}
return self;
}
FirstViewController.m
//归档对象
-(void)archiveObject
{
ABPersonInfo
*person = [[ABPersonInfo alloc]init];
person.name =
@"sqj";
person.age = [NSNumber
numberWithInt:12];
person.frame = [NSValue
valueWithCGPoint:CGPointMake(10, 20)];
NSData *data = [NSKeyedArchiver
archivedDataWithRootObject:person];
NSString *path = [self searchPath];
NSString *newPath =
[path stringByAppendingString:@"/person.txt"];
[data
writeToFile:newPath
atomically:YES];
}
//解档对象
-(void)unArchivedObject
{
NSString *path = [self searchPath];
NSString *newPath =
[path stringByAppendingString:@"/person.txt"];
NSData
*data = [NSData dataWithContentsOfFile:newPath];
//解档
ABPersonInfo *person = [NSKeyedUnarchiver
unarchiveObjectWithData:data];
NSLog(@"name == %@,age ==
%@,frame == %@",person.name,person.age,person.frame);
}
4.NSUserDefaults 系统的一个简单的本地存储 plist文件 存的东西比较小
#pragma mark -
#pragma mark NSUserDefaults
存储小型数据
//存储
-(void)writeUserDefaults
{
NSUserDefaults *object = [NSUserDefaults
standardUserDefaults];
//[object setObject:@"shenqingjun"
forKey:@"name"];//第一个参数 系统的对象
//添加内容
[object setObject:@"aishangyipiyema" forKey:@"xinqing"];
//同步存储
[object
synchronize];
}
//读取
-(void)readUserDefaules{
NSUserDefaults *object = [NSUserDefaults
standardUserDefaults];
NSString *name = [object
objectForKey:@"name"];
NSLog(@"name == %@",name);
}