数据写入并读取

demo截图
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    
#pragma mark ------------- 将数据写入本地文件并读取 -------------
    
    // 创建数组
    NSArray *arr = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
    // 将数组当中的元素写入数据文件当中(plist)
    if ([arr writeToFile:@"/Users/zhaoce/Desktop/arr.plist" atomically:YES])
    {
        NSLog(@"写入数据成功");
    }
    else
    {
        NSLog(@"写入数据失败");
    }
    // 创建一个数组,读取plist文件中的数据
    NSArray *arr1 = [NSArray arrayWithContentsOfFile:@"/Users/zhaoce/Desktop/arr.plist"];
    NSLog(@"arr1 ======%@", arr1);
    
    // 将两个字典放入plist文件中
    NSDictionary *readDic2 = @{@"Sg":@"shuguang",@"Gy":@"guoya",@"Dl":@"deliang",@"Jc":@"jiecheng"};
    NSDictionary *readDic3 = @{@"a":@"ab",@"b":@"bc",@"c":@"cd",@"d":@"de"};
    // 将两个字典放入一个数组当中
    NSArray *readArray = @[readDic2, readDic3];
    // 将数组写入到文件中
    [readArray writeToFile:@"/Users/zhaoce/Desktop/readArray.plist" atomically:YES];
    
    return 0;
}

推荐阅读更多精彩内容