NSBundle(工程目录)与沙盒路径的不同

创建好plist文件

这里写图片描述

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString* path = [[NSBundle mainBundle]pathForResource:@"Data.plist" ofType:nil];
    NSArray* arr = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"arr:%@",arr);
    //追加
    NSMutableArray* dataArr = [NSMutableArray arrayWithArray:arr];
    [dataArr addObject:@"F"];
    [dataArr addObject:@"H"];
    [dataArr writeToFile:path atomically:YES];
    NSArray* arr1 = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"arr1:%@",arr1);

    //获取Documents路经
    NSArray* pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docuPath = [pathArr objectAtIndex:0];
    //NSLog(@"Documents路径:%@",docuPath);
    //字符串路径追加文件路径
    NSString* filePath = [docuPath stringByAppendingPathComponent:@"Data.plist"];
    //读取文件内容
    NSArray* newArr = [NSArray arrayWithContentsOfFile:filePath];
    NSLog(@"newArr:%@",newArr);
    //通过这个方法写入内容时,如果文件不存在系统会自动创建此文件再写入数据,如果存在直接写入数据
    [dataArr writeToFile:filePath atomically:YES];
    NSArray* newArr2 = [NSArray arrayWithContentsOfFile:filePath];
    NSLog(@"newArr2:%@",newArr2);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值