2011-08-28 69 views
0

我创建了一个名为“医院”的plist,插入第一行作为一个数组,其中包含三个项目作为字符串。plists - iOS开发(NSString)

有什么错我的代码:

NSArray *hospitals = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Hospitals" ofType:@"plist"]]; 
    NSString *first = [[NSString alloc] initWithFormat:@"%@", [hospitals objectAtIndex:0]]; 
    NSLog(@"%@",first); 

这是给我的(空)的结果!

+0

描述性标题。 – 2011-08-28 10:48:50

回答

0

试试这个:

NSString *errorDesc = nil; 
NSPropertyListFormat format; 
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:[[NSBundle mainBundle] pathForResource:@"Hospitals" ofType:@"plist"]]; 
NSMutableDictionary *properties = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 
NSArray *hospitals = (NSArray *)[properties valueForKey:@"names"]; 
NSString *first = [hospitals objectAtIndex:0]; 
NSLog(@"%@",first); 
+0

这没有奏效! PLZ阅读最佳答案的评论! – user916316

+0

请把你的plist的内容放在这里。 – Nekto

+0

names - array - (3 items) item 0 - string - first item 1 - string - second item 2 - string - third – user916316

1

无论是属性列表文件不存在(检查“复制资源”建设目标的阶段),具有不同的名称(请注意,iOS版”文件系统的情况下敏感),根本不是属性列表(可能它有语法错误),不可读或属性列表的根不是数组,而是字典。

如果hospitals == nil以上任何一个都是问题所在。

+0

-i在我的(复制资源)中找到了plist -i也处理了区分大小写的事情, - plist root是一个数组! 我不知道有什么问题! &医院== nil为TRUE:S – user916316

+0

@ user916316尝试清理项目并重新构建它。 – 2011-08-28 11:10:01

+0

清洁也没有帮助! – user916316