IOS中沙盒的读写方式

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    //   h获取应用沙盒

    NSString*homaPath =  NSHomeDirectory();

    NSLog(@"%@",homaPath);

}

 

/**

 * 下面的是用  plist  方式 保存 数据

 */

-(void)savePlist{

    NSString*homePath =  NSHomeDirectory();

    NSString*filePath = [homePath stringByAppendingPathComponent:@"Docments"];

    

    //   指定文件格式

    NSString*format = [filePath stringByAppendingPathComponent:@"xx.plist"];

   NSArray *data = @[@"垃圾",@"垃圾"];

    

    [datawriteToFile:format atomically:YES];

    

    

    //   下面的是第二种方式---》系统提供的

    NSString*dataPath =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];

    NSString*filePathNssear = [dataPath stringByAppendingPathComponent:@"xx.plist"];

}

 

/**

 * 下面的是  plist 读取 数据

 */

-(void)readPlist{

    // 指定是哪个文件

    NSString*homePath = NSHomeDirectory();

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

    //   获取数据

    NSArray*data =  [NSArray arrayWithContentsOfFile:filePath];

}

 

 

//preference 方式  --->  偏好设置

-(void)preferenceSave{

    

   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    

    [udsetObject:@"value11" forKey:@"key1"];

    [udsetObject:@"value2" forKey:@"key2"];

    

    //   同步,一定要写这一步

    [udsynchronize];

}

/**

 * 下面是  preference  的读取方式

 */

-(void)preferenceRead{

   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    NSLog(@"%@",[ud objectForKey:@"key1"]);

    

}

/**

 * 数据的存储与读取 ====   >>>>>>   归档与解档

 */

-(void)keyedArchiverWrite{

    

    NSString*docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];

    NSString*filePath = [docPath stringByAppendingPathComponent:@"xx.data"];

    Teacher*teacher = [[Teacher alloc] init];

   teacher.name = @"垃圾";

   teacher.age = 10;

    

    //   归档

   [NSKeyedArchiver archiveRootObject:teacher toFile:filePath];

    

}

 

 

-(void)keyedUnarchiverRead{

    

    NSString*filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];

    NSString*documentType = [filePath stringByAppendingPathComponent:@"xx.data"];

    Teacher*taecher =  [NSKeyedUnarchiver unarchiveObjectWithFile:documentType];

    

}

 

 

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值