如何以编程方式将属性列表中的数据导入NSArray?

时间:2012-06-27 11:34:31

标签: iphone

我已经生成了一个属性列表,可以保存一些医院名称。现在我想从这个plist中将名字变成一个数组。请帮我。提前完成。

2 个答案:

答案 0 :(得分:4)

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

试试这个

答案 1 :(得分:0)

试试这个

NSMutableDictionary* dictionary = [[[NSMutableDictionary alloc] init] autorelease];

NSError *error;
// Look in Documents for an existing plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* metadataPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: @"Metadata.plist"]];
[metadataPlistPath retain];

// If it's not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:metadataPlistPath] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle]
pathForResource:@"Metadata" ofType:@"plist"];
[fileManger copyItemAtPath:pathToSettingsInBundle toPath:metadataPlistPath error:&error];
}
//Now read the plist file from Documents
NSString* myPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: @"Metadata.plist"] ];

dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:myPlistPath];

return dictionary;
}

//fetch the data based on key
- (NSMutableArray*) fetchMetadataArrayForKey:(NSString*)aKey {
NSMutableArray* arrCategories = [[[NSMutableArray alloc] init] autorelease];
NSMutableDictionary* dictionary = [self fetchMetadata];

arrCategories = [dictionary valueForKey:aKey];

return arrCategories;

}