#import <Foundation/Foundation.h>数组
#import "FJDog.h"安全
#define path @"/Users/IOS1601/Desktop/plist文件/plist1.plist"atom
#define path1 @"/Users/IOS1601/Desktop/plist文件/data.plist"spa
#define path2 @"/Users/IOS1601/Desktop/plist文件/plist2.plist".net
int main(int argc, const char * argv[]) {线程
@autoreleasepool {3d
//只能存数NSString,NSData,NSData,NSNumber,BOOLcode
//NSArray(NSMutableArray),NSDictionary(NSMutableDictionary) 其余类建立的对象不能存储orm
//包括系统其余的类对象
//=========建立plist文件(往plist文件里写数据)===========
//1.先建立一个数组或者一个字典(注意数组的数组元素和
//字典的键值对的值都只能是plist可以存储的类型);
NSDate * date = [NSDate date];//获取当前时间
NSArray * array = @[ @"哈哈",date,@1000 ,@NO];
//2.将数组写入plist文件中(若是这个文件不存在就会建立这个plist文件)
//若是这个plist文件已经存在,会修改plist文件的内容;
//参数1:文件路径(在这里只能是plist文件的文件路径)
//参数2:是不是原子操做(是否支持线程安全)
[array writeToFile:path atomically:NO];
//3.将字典写入plist文件中
NSDictionary *dictionary = @{@"77":@"luhan",
@"7":date
,@"777":array,
@"7777":@123};
[dictionary writeToFile:path atomically:NO];
//=========将plist文件的内容读取出来==============
//若是想要将plist文件读出来,就要知道plist文件最外层的结构
//(NSArray和NSDictionary);
NSDictionary * dict2 = [[NSDictionary alloc]
initWithContentsOfFile:path1];
//拿到字典中的数组
NSArray *arr = dict2[@"333"];
//再拿到数组的第一个元素:
NSString *name = arr[0];
NSLog(@"%@",name);
NSLog(@"%@",dict2);
//最外层是数组
NSArray *array2 = [NSArray arrayWithContentsOfFile:@"/Users/IOS1601/Desktop/plist文件/data1.plist"];
NSLog(@"%@",array2);
//=======plist不能存储其余的数据类型的对象=========
FJDog *dog = [[FJDog alloc]init];
dog.name = @"cat";
//若是数组或者字典中出现了plsit不能存储的类型的对象
//plist文件写入会失败;
NSArray *arr2 = @[@"abc",@777,dog];
[arr2 writeToFile:path2 atomically:NO];
}
return 0;
}