ios开发 过程中的简单的数据的存取

来源:互联网 发布:少女内裤品牌 知乎 编辑:程序博客网 时间:2024/05/18 08:52
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);} 

0 0
原创粉丝点击