[英]extract data from NSMutableArray
在我的应用程序中,我解析了一个xml,从中我获得了放入列表“ listElements”中的项目(酒店和餐馆)的列表。 我需要从该数组中获取另一个仅包含特定元素(仅酒店)的数组,然后我需要从该数组中获取三个包含根据星级的酒店的数组(array5starhotel,array4starhotel,array3starhotel)。 怎么办呢? 这是我用于解析的代码(我使用了TBXML):
- (void)loadCategories {
// instantiate an array to hold categories objects
listCategories = [[NSMutableArray alloc] initWithCapacity:100];
tbxml = [[TBXML tbxmlWithXMLFile:@"Molise.xml"] retain];
// Obtain root element
TBXMLElement * root = tbxml.rootXMLElement;
// if an author element was found
if (root) {
//search for the first category element within the root element's children
TBXMLElement * category = [TBXML childElementNamed:@"category" parentElement:root];
// if a category element was found
while (category != nil) {
// instantiate a category object
Category * aCategory = [[Category alloc] init];
// get the name attribute from the category element
aCategory.name = [TBXML valueOfAttributeNamed:@"name" forElement:category];
// search the category's child elements for a "element" element
TBXMLElement * element = [TBXML childElementNamed:@"element" parentElement:category];
// if a "element" element was found
while (element != nil) {
// instantiate a "element" object
Element * aElement = [[Element alloc] init];
// extract the attributes from the "element" element
aElement.title = [TBXML valueOfAttributeNamed:@"title" forElement:element];
aElement.address = [TBXML valueOfAttributeNamed:@"address" forElement:element];
aElement.stars =[TBXML valueOfAttributeNamed:@"stars" forElement:element];
// add the "element" object to the category's listElements array and release the resource
[aCategory.listElements addObject:aElement];
[aElement release];
// find the next sibling element named "element"
element = [TBXML nextSiblingNamed:@"element" searchFromElement:element];
}
// add our category object to the listCategories array and release the resource
[listaCategories addObject:aCategory];
[aCategory release];
// find the next sibling element named "category"
category = [TBXML nextSiblingNamed:@"category" searchFromElement:category];
}
[tbxml release];
}
好的,所以您的代码段不执行问题说明的操作。
您创建了一个包含很多Category对象的类别数组(listCategories)。 每一个都包含一个listElements数组。
假设您要使用“酒店”类别中的商品。 。 。
// Get the hotels category
Category *hotelsCategory = nil;
for (Category *temp in listCategories.each) {
if ([temp.name isEqualToString:@"hotel"]) {
hotelsCategory = temp;
break;
}
}
if (nil == hotelsCategory)
return NSLog(@"No hotels category found");
// Get the hotels from this category with a 3 or above star rating
NSMutableArray *array5starhotel = [NSMutableArray array];
NSMutableArray *array4starhotel = [NSMutableArray array];
NSMutableArray *array3starhotel = [NSMutableArray array];
for (Element *element in hotelsCategory.listElements) {
if ([element.stars isEqualToString:@"5"])
[array5starhotel addObject:element];
if ([element.stars isEqualToString:@"4"])
[array4starhotel addObject:element];
if ([element.stars isEqualToString:@"3"])
[array3starhotel addObject:element];
}
希望有帮助-我不得不猜测一些东西,因为我不知道Category或Element对象中的内容!
NSArray
的[filteredArrayUsingPredicate:][1]
是作业的工具。
如前所述,您的listCategories
数组将包含Category
对象的数组,每个类别均包含Element
对象的数组。
// Retrieve "hotel" Elements
NSPredicate *hotelPredicate = [NSPredicate predicateWithFormat:@"name == hotel"];
Category *hotelCategory = [[[listCategories filteredArrayUsingPredicate:hotelPredicate] objectAtIndex: 0] retain];
NSArray *hotels = [[hotelCategory listElements] retain];
// Split the array of Elements based on star value
NSPredicate *elementRatingTemplate = [NSPredicate predicateWithFormat:@"stars == $STARS"];
NSPredicate *threeStarPredicate = [elementRatingTemplate predicateWithSubstitutionVariables: [NSDictionary dictionaryWithObject:@"3" forKey:@"STARS"];
NSArray *threeStarHotels = [[hotels filteredArrayUsingPredicate: threeStarPredicate] retain];
NSPredicate *fourStarPredicate = [elementRatingTemplate predicateWithSubstitutionVariables: [NSDictionary dictionaryWithObject:@"4" forKey:@"STARS"];
NSArray *fourStarHotels = [[hotels filteredArrayUsingPredicate: fourStarPredicate] retain];
NSPredicate *fiveStarPredicate = [elementRatingTemplate predicateWithSubstitutionVariables: [NSDictionary dictionaryWithObject:@"5" forKey:@"STARS"];
NSArray *fiveStarHotels = [[hotels filteredArrayUsingPredicate: fiveStarPredicate] retain];
// Do something with the filtered arrays
// Clean up retained objects.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.