سؤال

NSString* path = @"http://username:apicode@flightxml.flightaware.com/json/FlightXML2/AirlineFlightSchedules?startDate=1394424000&endDate=1394486000&origin=KLAX&destination=KJFK&airline=UAL&howMany=10&offset=0";


    NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];

    [_request setHTTPMethod:@"GET"];


    NSURLResponse *response = nil;

    NSError *error = nil;


    NSData* _connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];

    if(nil != error)
    {
        NSLog(@"Error: %@", error);
    }
    else
    {

        NSMutableDictionary* json = nil;


        if(nil != _connectionData)
        {
            json = [NSJSONSerialization JSONObjectWithData:_connectionData options:NSJSONReadingMutableContainers error:&error];
        }

        if (error || !json)
        {
            NSLog(@"Could not parse loaded json with error:%@", error);
        }
        else
        {

            NSMutableDictionary *routeRes;

            routeRes = [json objectForKey:@"AirlineFlightSchedulesResult"];

            NSMutableArray *res;

            res = [routeRes objectForKey:@"data"];




            for(NSMutableDictionary *flight in res)
            {

                NSLog(@"ident is %@, aircrafttype is %@, originName is %@, origin is %@,%@,%@,%@,%@,%@,%@,%@", [flight objectForKey:@"actual_ident"], [flight objectForKey:@"aircrafttype"], [flight objectForKey:@"arrivaltime"], [flight objectForKey:@"departuretime"], [flight objectForKey:@"destination"], [flight objectForKey:@"ident"], [flight objectForKey:@"meal_service"], [flight objectForKey:@"origin"], [flight objectForKey:@"seats_cabin_business"], [flight objectForKey:@"seats_cabin_coach"], [flight objectForKey:@"seats_cabin_first"]);





            }

        }

        _connectionData = nil;
        NSLog(@"connection nil");
    }

}

}

I get data on flights from an external api, but how do I get the returned flights in an NSMutable Dictionary to write on tableview cells? I would like each flight returned to become its own tableview cell. Any help is appreciated.

هل كانت مفيدة؟

المحلول

You are fetching JSON, and then parsing it. OK.

You have to know your JSON layout, and then just pick your data from the resulting object. It looks like you are doing something like that. However, you are assigning the result to mutable objects. You can't do that as those API return immutable objects.

If you want a mutable version, you need to call mutableCopy...

NSMutableDictionary *routeRes;
routeRes = [[json objectForKey:@"AirlineFlightSchedulesResult"] mutableCopy];

This, of course, assumes that your JSON is holding a dictionary for the key "AirlineFlightSchedulesResult". There is no explicit check being done for you. It is assumed that you know what type of object you are asking for.

You will then just need to convert your data into an array, where you have one item in the array for each item you want in the table view, and use the standard table view API.

However, from your code, it does not look like you really want a mutable dictionary, you just want to put stuff into the array. Consider this...

NSMutableArray results = [NSMutableArray array];
NSDictionary *routeRes = [json objectForKey:@"AirlineFlightSchedulesResult"];
NSArray *res [routeRes objectForKey:@"data"];
for(NSDictionary *flight in res)
{
    // Put whatever you want into your array.
    [results addObject:[MyFlight flightWithDictionary:flight]];
}

Now, results will be an array of MyFlight objects, each created by the factory method flightWithDictionary.

نصائح أخرى

NSLog the JSON response you get:

NSLog(@"json = %@", [json description]);

and then read my answer to THIS POST. All you need to do is know the structure of what you are getting back from that external API. You will want to set your UITableView datasource to the array of flight dictionaries.

NSArray *flights = [res allValues];

Will get you an array with the data. Then just setup the tableview with the delegate methods.

E.g.
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can:
NSMutableDictionary *flight = flights[indexPath.row]; cell.flightIdent.text = flight[@"ident];

Assuming that your cell has an property of UILabel named FlightIdent.

a few steps :

  1. creat UITableView, set UITableViewDelegate and UITableViewDataSource;

  2. when you get data on flights from an external api ,call [YourTableView reloadData];

  3. implementation - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:        (NSInteger)section
    {
         NSArray *arAllFlights = [res allValues];
         return [arAllFlights count];
    }
    
  4. implementation - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSMutableDictionary *dicFlight = arAllFlights[indexPath.row];
        cell.flightIdent.text = dicFlight[@"key"];
        return cell.
    }
    

hope this will help you. Thanks!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top