如何从NSMutableArray中提取值?

时间:2010-08-31 05:08:50

标签: iphone

-(void) readRestaurantFromXml {  
    SGAAppDelegate *app = (SGAAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *myRequestString = [NSString stringWithFormat:@"%@getXml.aspx",app.SERVER_URL];    
    NSURL *url = [NSURL URLWithString: myRequestString];
    CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];
    resultNodes = [rssParser nodesForXPath:@"//item" error:nil];    
    listOfItems = [[NSMutableArray alloc] init];
    for (CXMLElement *resultElement in resultNodes) {
        NSString *theatre_id = [[resultElement childAtIndex:0] stringValue];
        NSString *theatre_name = [[resultElement childAtIndex:1] stringValue];
        NSString *restaurant_id  = [[resultElement childAtIndex:2] stringValue];
        NSString *restaurant_name  = [[resultElement childAtIndex:3] stringValue];
        NSString *restaurant_desc = [[resultElement childAtIndex:4] stringValue];
        NSString *deal_avail = [[resultElement childAtIndex:5] stringValue];
        NSString *deal_details = [[resultElement childAtIndex:6] stringValue];      
        Restaurant *workVal = [[Restaurant alloc] initWithRestTitle:restaurant_name restDesc:restaurant_desc theatreId:theatre_id theatreName:theatre_name restId:restaurant_id restDeals:deal_avail restDealDesc:deal_details];
        [listOfItems addObject:workVal];            
        [workVal release];      
    }

}
-(void)myTask{
   NSLog(@"%d",[listOfItems count]);
}

NSMutableArray listofItems有3个值。那么如何在另一个方法中提取数组值呢?

帮帮我

2 个答案:

答案 0 :(得分:1)

快速枚举是首选方法。

for (id object in listOfItems) {
  //Do something...
}

如果您想在索引处使用单个对象:

id object = [listOfItems objectAtIndex:someIndex];

如果你只想要一个对象(期望数组有一个对象:

id object = [listOfItems lastObject];

答案 1 :(得分:0)

for(int i=0; i < [listOfItems count]; ++i) {
    Restaurant* r = [listofItems objectAtIndex:i];
}