0

我生成了一个属性列表,其中保存了一些医院名称。现在我想从该 plist 中将名称放入一个数组中。请帮我。提前谢谢。

4

2 回答 2

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

尝试这个

于 2012-06-27T11:35:38.713 回答
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;

}
于 2012-06-27T11:45:50.157 回答