gpt4 book ai didi

ios - 从 .plist 文件中读取

转载 作者:行者123 更新时间:2023-11-28 22:11:52 25 4
gpt4 key购买 nike

有个小问题。我有一个包含下一个数据的 .plist 文件 http://imageshack.com/a/img856/1421/xvzm.png

所以我无法理解,我需要先读取数组,而不是数组读取字典。或者可能需要重写文件并将 Root 键更改为类型字典,然后这样读取:

NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
if (!temp) {
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
self.personName = [temp objectForKey:@"Name"];
self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];

最佳答案

可以通过以下代码实现:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSArray *dataArray = [NSArray arrayWithContentsOfFile:plistPath];

这个数组将包含所有的字典,你可以像这样获取它们:

NSDictionary *dict = [dataArray objectAtIndex:0];

关于ios - 从 .plist 文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22722437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com