Trying to convert object to NSMutableArray

Discussion in 'iOS Programming' started by johnmerlino, Nov 9, 2011.

  1. johnmerlino, Nov 9, 2011
    Last edited by a moderator: Nov 9, 2011

    johnmerlino macrumors member

    Joined:
    Oct 22, 2011
    #1
    Hey all,

    I'm using JSONKit.

    Code:
    NSDictionary *resultsDictionary = [response objectFromJSONString];
            NSLog(@"the value is %@", resultsDictionary);
            units = [resultsDictionary allValues];


    That NSLog outputs this:

    Code:
    the value is (
            {
            unit =         {
                "account_id" = 66;
                "act_exp_date" = "<null>";
                "activation_state" = 2;
                "airtime_plan" = 1;
                "created_at" = "<null>";
                enabled = 1;
                "firmware_version" = "<null>";
                "gateway_ip" = "<null>";
                "hardware_revision" = "<null>";
                icon = "<null>";
                id = 1111;
                "is_speeding" = 0;
                name = 1111;
                "sim_number" = "<null>";
                "sim_phone" = "<null>";
                "speed_limit" = "<null>";
                "speed_limit_enabled" = 0;
                "unit_group_id" = "<null>";
                "unit_type_id" = 2;
                "updated_at" = "2011-10-31T15:26:31Z";
            };
        },
            {
            unit =         {
                "account_id" = 2;
                "act_exp_date" = "2011-09-23";
                "activation_state" = 2;
                "airtime_plan" = 1;
                "created_at" = "2011-09-06T16:10:38Z";
                enabled = 1;
                "firmware_version" = "<null>";
                "gateway_ip" = "<null>";
                "hardware_revision" = "<null>";
                icon = "<null>";
                id = 1113;
                "is_speeding" = 0;
                name = 1113;
                "sim_number" = "<null>";
                "sim_phone" = "<null>";
                "speed_limit" = "<null>";
                "speed_limit_enabled" = 0;
                "unit_group_id" = 1;
                "unit_type_id" = 1;
                "updated_at" = "2011-10-25T13:22:21Z";
            };
        },
            {
            unit =         {
                "account_id" = 2;
                "act_exp_date" = "2011-01-01";
                "activation_state" = 2;
                "airtime_plan" = 1;
                "created_at" = "2011-09-26T16:35:45Z";
                enabled = 1;
                "firmware_version" = "<null>";
                "gateway_ip" = "<null>";
                "hardware_revision" = "<null>";
                icon = "";
                id = 1114;
                "is_speeding" = 0;
                name = 1114;
                "sim_number" = "<null>";
                "sim_phone" = "<null>";
                "speed_limit" = "<null>";
                "speed_limit_enabled" = 0;
                "unit_group_id" = "<null>";
                "unit_type_id" = 2;
                "updated_at" = "2011-10-25T13:22:21Z";
            };
        },
            {
            unit =         {
                "account_id" = 2;
                "act_exp_date" = "2012-01-01";
                "activation_state" = 2;
                "airtime_plan" = 1;
                "created_at" = "2011-09-26T16:37:52Z";
                enabled = 1;
                "firmware_version" = "<null>";
                "gateway_ip" = "<null>";
                "hardware_revision" = "<null>";
                icon = "";
                id = 1115;
                "is_speeding" = 0;
                name = 1115;
                "sim_number" = "<null>";
                "sim_phone" = "<null>";
                "speed_limit" = "<null>";
                "speed_limit_enabled" = 0;
                "unit_group_id" = "<null>";
                "unit_type_id" = 1;
                "updated_at" = "2011-10-25T13:22:21Z";
            };
        }
    )
    
    
    However, since I want to populate this data in a table, I want to convert it to an NSMutable array.

    But when I call this:

    Code:
    units = [resultsDictionary allValues];
    When the application runs, I get an error:

    Code:
     *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JKArray allValues]: unrecognized selector sent to instance 0x6ba9b00'
    *** First throw call stack:
    (0x1608052 0x1799d0a 0x1609ced 0x156ef00 0x156ece2 0x1aa7a 0x6d364e 0x6d2c1c 0x6f956d 0x6e3d47 0x6fa441 0x6fa45d 0x6fa4f9 0x63ad65 0x63afe8 0x6f9a8a 0x9ece65 0x9e4940 0x9ee014 0x9e186d 0x1609ec9 0x6105c2 0x61055a 0x6b5b76 0x6b603f 0x6b52fe 0x635a30 0x635c56 0x61c384 0x60faa9 0x1d18fa9 0x15dc1c5 0x1541022 0x153f90a 0x153edb4 0x153eccb 0x1d17879 0x1d1793e 0x60da9b 0x28a8 0x2805)
    terminate called throwing an exception

    thanks for response
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Seems to me your response is already an array (of unit dictionaries).
     
  3. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    JsonKit, provides arrays of dictionarys.
    So best is to turn your units in an array.
    You need to do something like this
    Code:
    for (NSDictionary *dict in units) {
     Model *model = [[Model alloc] init];
     model.property = [dict objectForKey:@"stringName"];
     model.property2 = [dict objectForKey:@"stringName2"];
     [SingletonArray addObject:model];
     [model release];
    }
    Hope that helped you out in some way.
     
  4. johnmerlino thread starter macrumors member

    Joined:
    Oct 22, 2011
    #4
    thanks for response, I dont understand what SingletonArray is supposed to be. Is that just supposed to be an NSMutableArray that I create?
     
  5. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    I normally use an Singleton to make a manager that will hold all my NSMutableArrays, but yes, that's the idea.
     
  6. sathishjdev, Apr 29, 2014
    Last edited by a moderator: Apr 29, 2014

    sathishjdev macrumors newbie

    Joined:
    Apr 29, 2014
    #6
    Solve using try catch

    Code:
    @try {
                    resultArray = [[NSMutableArray alloc]initWithArray:[statusInDictionary objectForKey:@"testseting"]];
                }
                @catch (NSException *exception) {
                    resultArray = [[NSMutableArray alloc]initWithObjects:[statusInDictionary objectForKey:@"test"],nil];
                }
                @finally {
                    NSLog(@"resultArray  %@",resultArray );
                }
     

Share This Page