首页 > 其他分享 >IOS之【属性列表】

IOS之【属性列表】

时间:2022-12-09 15:00:41浏览次数:43  
标签:stringByAppendingPathComponent documents IOS 列表 NSString dict path plist 属性

@implementation

- (void)viewDidLoad
{
[super viewDidLoad];

[self writePerson];
}

尝试写Person
// 不能通过writeToFile将一个普通对象写入文件中
// writeToFile会删掉以前存在的文字,创建一个新的文件
- (void)writePerson {
Person *person = [[[Person alloc] init] autorelease];
name = @"JamesWong";
age = 10;

//NSArray *array = [NSArray arrayWithObject:person];

// 注意:第一个参数是NSDocumentDirectory,说明要搜索Documents目录
// NSUserDomainMask:在应用沙盒中搜索
// 如果第3个参数写NO:~/Documents
// 在iOS平台,这个函数返回的数组中只有1个结果
//NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *path = [documents stringByAppendingPathComponent:@"array.plist"];

NSArray *array = [NSArray arrayWithObjects:@"1", nil];
[array writeToFile:path atomically:YES];
}

将字典写入属性列表文件中
- (void)writeDict {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"JamesWong" forKey:@"name"];
[dict setObject:[NSNumber numberWithInt:10] forKey:@"age"];

// 获取应用沙盒的根路径
NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
// 属性列表的默认拓展名是plist
NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];

[dict writeToFile:path atomically:YES];
}

从属性列表文件中读取字典
- (void)readDict {
// 获取应用沙盒的根路径
NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
// 属性列表的默认拓展名是plist
NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

NSLog(@"%@", dict);
}
@end

标签:stringByAppendingPathComponent,documents,IOS,列表,NSString,dict,path,plist,属性
From: https://blog.51cto.com/u_15907570/5925326

相关文章