1,常用目录的获取
//沙盒所在的根目录
NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);
//应用程序路径
NSString*appPath = [[NSBundle mainBundle] resourcePath];
NSLog(@"path:%@", appPath);
//document目录
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPaths = [docPaths objectAtIndex:0];
NSLog(@"path:%@", documentPaths);
//Cache目录
NSArray *cacPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPaths objectAtIndex:0];
NSLog(@"%@", cachePath);
//获取Library目录
NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryPath = [libPaths objectAtIndex:0];
NSLog(@"%@", libraryPath);
//获取Tmp目录
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"%@", tmpDir);
2015-11-29 23:27:54.904 file[1338:162595] path:/Users/wang
2015-11-29 23:27:54.904 file[1338:162595] path:/Users/wang/Library/Developer/Xcode/DerivedData/file-clrabutrnxrhkncilvhdiysmqvir/Build/Products/Debug/file.app/Contents/Resources
2015-11-29 23:27:54.904 file[1338:162595] path:/Users/wang/Documents
2015-11-29 23:27:54.904 file[1338:162595] /Users/wang/Library/Caches
2015-11-29 23:27:54.904 file[1338:162595] /Users/wang/Library
2015-11-29 23:27:54.905 file[1338:162595] /var/folders/gg/cc6n2tms01j48j3l9g6sph100000gp/T/
2,利用系统进行沙盒存储,
//利用系统进行数据的归档和解档操作
NSArray *arr = [NSArray arrayWithObjects:@1,@"你好",@3, nil];
NSString* filePath=[NSHomeDirectory() stringByAppendingPathComponent:@"test2.txt"];
//归档数据
BOOL success= [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
//接档
NSArray *array=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
for (NSString *str1 in array){
NSLog(@"%@",str1);
}
2015-11-29 23:30:01.239 file[1352:163601] 1
2015-11-29 23:30:01.239 file[1352:163601] 你好
2015-11-29 23:30:01.239 file[1352:163601] 3
3,沙盒存储不同类型的数据
//文件归档操作
NSArray *array = [NSArray arrayWithObjects:@1,@2,@"中文", nil];
NSInteger integer = 10;
BOOL archiverBool = YES;
/*------对象归档--------------------*/
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//对多个对象编码归档,key是解档时对象的标识符,可任意编写
//encode有多个重载,但并不是任何系统对象都有重载方法
[archiver encodeObject:array forKey:@"arr"];
[archiver encodeInteger:integer forKey:@"integer"];
[archiver encodeBool:archiverBool forKey:@"archiverBool"];
//完成编码
[archiver finishEncoding];
NSString *filepath = [NSHomeDirectory() stringByAppendingPathComponent:@"1.txt"];
//将data以原子操作方式写入文件
BOOL success = [data writeToFile:filepath atomically:YES];
if (success) {
NSLog(@"归档成功");
}
/*------对象归档--------------------*/
/*------对象解档--------------------*/
//将文件数据加载入NSData对象
NSData *data2 = [NSData dataWithContentsOfFile:filepath];
//使用NSData对象初始化接档器
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data2];
//根据归档时的类型和key解档对应数据
NSArray *arr2 = [unarchiver decodeObjectForKey:@"arr"];
NSInteger integer2 = [unarchiver decodeIntegerForKey:@"integer"];
BOOL unarchiveBool = [unarchiver decodeBoolForKey:@"archiveBool"];
NSLog(@"%@",arr2);
NSLog(@"%ld",integer2);
NSLog(@"%d",unarchiveBool);
/*------对象接档--------------------*/
2015-11-29 23:31:17.078 file[1366:164679] 归档成功
2015-11-29 23:31:17.079 file[1366:164679] (
1,
2,
"\U4e2d\U6587"
)
2015-11-29 23:31:17.079 file[1366:164679] 10
2015-11-29 23:31:17.079 file[1366:164679] 0
自定义归档数据,以前再总结