简体   繁体   中英

How to extract the value from NSMutableArray?

-(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]);
}

The NSMutableArray listofItems has 3 values.So how can i extract the array value in another method?

Help me

Fast enumeration is the preferred method.

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

If you want a single object at an index:

id object = [listOfItems objectAtIndex:someIndex];

If you just want an object (expecting the array to have a single object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM