iOS沙盒结构分析:
应用程序包:(Bundle)包含了所有的资源文件和可执行文件
Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录
Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积较大、不需要备份的非重要数据
Library/Preference:保存应用的所有偏好设置,iOS的设置应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录
tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录
几种目录获取方式:
NSString *home = NSHomeDirectory();//主目录
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = documents[0];//文档目录
NSArray *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, NO);
NSString *cacheDir = caches[0];//缓存目录
NSString *tmpDir = NSTemporaryDirectory();//临时目录
- (IBAction)pilstSave
{
//获取沙盒路径
NSString *path = NSHomeDirectory();
NSLog(@"%@" , NSTemporaryDirectory());
NSString *documentPath = [path stringByAppendingPathComponent:@"Documents"];
NSArray *date = @[@"temp1" , @"temp2"];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"date.plist"];
[date writeToFile:filePath atomically:YES];
}
- (IBAction)plistGet
{
NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"date.plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@",array);
}
//偏好的存取
- (IBAction)pianhaoSave
{
NSUserDefaults *defau = [NSUserDefaults standardUserDefaults];
[defau setObject:@"xxb" forKey:@"name"];
[defau setInteger:13 forKey:@"age"];
//立即同步
[defau synchronize];
}
- (IBAction)pianhaoGet
{
NSUserDefaults *defau = [NSUserDefaults standardUserDefaults];
NSString *name = [defau objectForKey:@"name"];
NSInteger age = [defau integerForKey:@"age"];
NSLog(@"%@ %d",name ,age);
}
/*
NSCoding 的存取(ps:需要遵守<NSCoding>协议,需要重写
- (void)encodeWithCoder:(NSCoder *)aCoder
和
- (id)initWithCoder:(NSCoder *)aDecoder)
方法
*/
#import "XXBPerson.h"
@implementation XXBPerson
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init])
{
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
- (IBAction)nscodingSave
{
XXBPerson *p1 = [[XXBPerson alloc] init];
p1.name = @"name1";
p1.age = 13;
XXBPerson *p2 = [[XXBPerson alloc] init];
p2.name = @"name2";
p2.age = 23;
NSString *home = NSHomeDirectory();
NSString *documentPath = [home stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"person.date"];
[NSKeyedArchiver archiveRootObject:@[p1,p2] toFile:filePath];
// [NSKeyedArchiver archiveRootObject:p2 toFile:filePath];
}
存取数据
- (IBAction)nscodingGet
{
NSString *home = NSHomeDirectory();
NSString *documentsPath = [home stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"person.date"];
NSArray *temp = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
XXBPerson *p1 = temp[0];
XXBPerson *p2 = temp[1];
NSLog(@"%@ %d",p1.name ,p1.age);
NSLog(@"%@ %d",p2.name ,p2.age);
} ios开发 过程中的简单的数据的存取
最新推荐文章于 2024-05-23 10:42:26 发布
本文详细介绍了iOS应用程序的沙盒结构,包括应用程序包、Documents、Library/Caches等目录的用途及数据存储方式,并提供了获取这些目录路径的方法。
843

被折叠的 条评论
为什么被折叠?



