IOS学习 沙盒位置,沙盒存储的三种方式:plist文件,偏好设置,归档

来源:互联网 发布:mac玩梦幻西游快捷键 编辑:程序博客网 时间:2024/04/28 06:15

在Mac OS 10.10.5和Xcode7.2的环境下

沙盒的位置:

/Users/liaojianguo/Library/Developer/CoreSimulator/Devices/C07367C4-84A0-4CA2-9FFB-DAB3B7C8CB81/data/Containers/Data/Application/20D764F7-EAC2-4375-909B-99409CD0B2EC/Documents


注:红色字体的路径表示设备的路径

        蓝色字体的路径表示应用的路径


===


默认情况下,每个沙盒含有3个文件夹:Documents、Library 和 tmp。

Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下;
Library:存储程序的默认设置或其它状态信息;
tmp:提供一个即时创建临时文件的地方。

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [selfdemo8];

}


//解归档

-(void)demo8{

    //获取路径

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];

    

    //拼接文件名

    NSString *filePath = [pathstringByAppendingPathComponent:@"person.plist"];

    

    //读取数据

    Person *person = [NSKeyedUnarchiverunarchiveObjectWithFile:filePath];

    

    NSLog(@"name = %@,phone = %@",person.name,person.phone);

}


//用归档的方式存储:可能存储任何格式文件

-(void)demo7{

    //创建Person对象

    Person *person = [[Personalloc]init];

    

    //person赋值

    person.name =@"longfeng";

    person.phone =@"123456";

    

    //获取document路径

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];

    

    //拼接路径

    NSString *filePath = [pathstringByAppendingPathComponent:@"person.plist"];

    

    //通过归档的方式存储

    [NSKeyedArchiverarchiveRootObject:persontoFile:filePath];

    NSLog(@"%@",filePath);

    

}


//读取偏好设置

-(void)demo6{

    //创建NSUserDefaults对象

    NSUserDefaults * userDefaults = [NSUserDefaultsstandardUserDefaults];

    

    //获取数据

    BOOL isAuto = [userDefaultsboolForKey:@"isAutologin"];

    NSString * name = [userDefaultsobjectForKey:@"name"];

    NSString * pwd = [userDefaultsobjectForKey:@"pwd"];

    

    NSLog(@"%d---%@----%@",isAuto,name,pwd);


}


//偏好设置存储:一般存储用户名、密码等重要的小数据信息

-(void)demo5{

    //创建NSUserDefaults对象

    NSUserDefaults * userDefaults = [NSUserDefaultsstandardUserDefaults];

    

    //设置数据

    [userDefaults setBool:YESforKey:@"autoLogin"];

    [userDefaults setObject:@"ZLF"forKey:@"name"];

    [userDefaults setObject:@"123"forKey:@"pwd"];

    

    //立即存储

    [userDefaults synchronize];

    NSLog(@"%@",NSHomeDirectory());

}


//将沙盒中name.plist文件读出来

-(void)demo4{

    //1.获取路径

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject];

    

    //2.拼接路径

    NSString *filePath = [pathstringByAppendingPathComponent:@"name.plist"];

    

    //3.读取数据

    NSArray *array = [NSArrayarrayWithContentsOfFile:filePath];

    NSLog(@"%@",array);

}


//将数组以plist文件格式存入沙盒

-(void)demo3{

    //1.创建数据

    NSArray *array = [NSArrayarrayWithObjects:@"jianguo",@"yihong",@"longfeng",@"youyou",@"多多",nil];

    

    //2.获取documents路径

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject];

    

    //3.拼接文件名

    NSString *filePath = [pathstringByAppendingPathComponent:@"name.plist"];

    

    //4.存储参数2:是否允许原子型写入

    [array writeToFile:filePathatomically:YES];

    NSLog(@"%@",filePath);

}


-(void)demo2{

    //获取caches文件夹:用于存放临时数据,例:下载的图片,需要管理的内存,

    

    NSString * caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) lastObject];

    

    NSLog(@"%@",caches);

}


-(void)demo1{

    //获取沙盒路径

    NSLog(@"沙盒路径:%@",NSHomeDirectory());

    

    //获取Bundle路径

    NSLog(@"boundle路径:%@",[NSBundlemainBundle].bundlePath);

    

    //获取Documents文件夹:长久存放的大数据,例:通讯录

    //1.获取沙盒路径

//    NSString *homePath = NSHomeDirectory();

    

    //2.拼接字符串 (方式一)

//    NSString *documentPath = [homePath stringByAppendingString:@"/Documents"];

    

    //方式二:作为路径的一部分

//    NSString *documentPath = [homePath stringByAppendingPathComponent:@"Documents"];

    

    //方式三参数1:目标文件夹参数2:作用域参数3:是否隐藏或显示全路径

    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];

    NSLog(@"%@",documentPath);

    

}



#import <Foundation/Foundation.h>


@interface Person :NSObject<NSCoding>

@property(nonatomic,retain)NSString *name;

@property(nonatomic,retain)NSString *phone;


@end



@implementation Person

//归档 告诉系统要存储对象的哪些属性

- (void)encodeWithCoder:(NSCoder *)aCoder

{

    [aCoder encodeObject:_nameforKey:@"name"];

    [aCoder encodeObject:_phoneforKey:@"phone"];

}


//解归档 读取对象的哪些属性

- (instancetype)initWithCoder:(NSCoder *)aDecoder

{

    if (self = [superinit])

    {

        self.name = [aDecoderdecodeObjectForKey:@"name"];

        self.phone = [aDecoderdecodeObjectForKey:@"phone"];

    }

    returnself;

}

@end

0 0
原创粉丝点击