簡體   English   中英

從.plist文件中讀取

[英]read from .plist file

有點問題。 我有一個包含下一個數據的.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];
SString *plistFile = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistFile];

for(NSDictionary *dictionary in array) {
    NSLog(@"%@", dictionary);
}

NSDictionary *item0 = array[0];
NSString *imageName = item[0][@"name"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM