Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7188157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:00:28+00:00 2026-05-28T19:00:28+00:00

I am creating a multidimensional array for sections / rows based on json data

  • 0

I am creating a multidimensional array for sections / rows based on json data from our API. Looking at the logs the adding rows and sections look good but when I nslog the sections it only shows a bunch of the last object. It seems like the rows are not being added to sections.

What am I doing wrong to make the last API object show in the sections x times? x represents the json count. self.appointments is an NSArray *

According to objective-c multi-dimensional array it should work.

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSMutableArray *sections = [[NSMutableArray alloc] init];
        NSMutableArray *rows = [[NSMutableArray alloc] init];
        NSString *lastDate = nil;
        for (NSDictionary *dict in [json objectForKey:@"data"]) {
            NSString *date = [dict objectForKey:@"date"];
            NSLog(@"Dates: %@ - %@", date, lastDate);
            if (date != lastDate) {
                if (lastDate == nil) {
                    NSLog(@"Adding Row 1");
                    [rows addObject:dict];
                    lastDate = date;
                } else {
                    NSLog(@"Adding Section 1");
                    NSLog(@"Adding #rows %i",[rows count]);
                    [sections addObject:rows];
                    [rows removeAllObjects];
                    NSLog(@"Adding Row 2 %@",[dict objectForKey:@"start_time"]);
                    [rows addObject:dict];
                    lastDate = date;
                }
            } else {
                NSLog(@"Adding Row 3");
                [rows addObject:dict];
            }
        }
        if (rows) {
            NSLog(@"Adding Section 2");
            NSLog(@"Adding #rows %i",[rows count]);
            [sections addObject:rows];
        }
        NSLog(@"Sections: %@", sections);

        self.appointments = [sections mutableCopy]; //I have also tried self.appointments = sections

        sections = nil;
        rows = nil;

Logs show

Sections: (
        (
                {
            abbrev = "";
            account = "";
            "addOnService_id" = "";
            alert = "";
            "appt_id" = 1839384;
            "appt_id_unique" = 1839384;
            "appt_status_description" = "";
            "appt_status_type" = "";
            "c_id" = 47;
            cost = "0.00";
            "coupon_id" = "";
            "creation_emp_id" = 2288;
            "creation_timestamp" = 201111040717;
            "customer_id" = 0;
            "customer_notes" = "";
            "customer_package_id" = "";
            date = 20121228;
            "employee_id" = 2288;
            "employee_notes" = "";
            employer = "";
            "end_time" = 570;
            "first_name" = "";
            "history_id" = 1830959;
            key = 134;
            "last_emp_id" = 2288;
            "last_name" = "";
            "last_timestamp" = 201111040717;
            "lead_description" = "";
            "link_id" = 0;
            "location_name" = "Telephonic Setup/Training";
            "make_id" = "";
            "middle_name" = "";
            "model_id" = "";
            "model_year" = "";
            name = "My Name ";
            odometer = "";
            "other_vehicle" = "";
            "package_name" = "";
            "payment_type_description" = "";
            "payment_type_id" = "";
            "pet_id" = "";
            "po_number" = "";
            reason = "B.O.B";
            "rebook_id" = "";
            "recur_id" = 20954;
            "rep_id" = "";
            "room_id" = 0;
            "room_name" = "";
            service = "";
            "service_id" = 0;
            "service_time_description" = "";
            spots = 1;
            "staff_screen_name" = "John Smith";
            "staff_type_id" = 0;
            "start_time" = 540;
            "status_id" = 0;
            tip = "";
            "type_id" = 8;
            vin = "";
        }
    ),
        (
                {
            abbrev = "";
            account = "";
            "addOnService_id" = "";
            alert = "";
            "appt_id" = 1839384;
            "appt_id_unique" = 1839384;
            "appt_status_description" = "";
            "appt_status_type" = "";
            "c_id" = 47;
            cost = "0.00";
            "coupon_id" = "";
            "creation_emp_id" = 2288;
            "creation_timestamp" = 201111040717;
            "customer_id" = 0;
            "customer_notes" = "";
            "customer_package_id" = "";
            date = 20121228;
            "employee_id" = 2288;
            "employee_notes" = "";
            employer = "";
            "end_time" = 570;
            "first_name" = "";
            "history_id" = 1830959;
            key = 134;
            "last_emp_id" = 2288;
            "last_name" = "";
            "last_timestamp" = 201111040717;
            "lead_description" = "";
            "link_id" = 0;
            "location_name" = "Telephonic Setup/Training";
            "make_id" = "";
            "middle_name" = "";
            "model_id" = "";
            "model_year" = "";
            name = "My Name ";
            odometer = "";
            "other_vehicle" = "";
            "package_name" = "";
            "payment_type_description" = "";
            "payment_type_id" = "";
            "pet_id" = "";
            "po_number" = "";
            reason = "B.O.B";
            "rebook_id" = "";
            "recur_id" = 20954;
            "rep_id" = "";
            "room_id" = 0;
            "room_name" = "";
            service = "";
            "service_id" = 0;
            "service_time_description" = "";
            spots = 1;
            "staff_screen_name" = "John Smith";
            "staff_type_id" = 0;
            "start_time" = 540;
            "status_id" = 0;
            tip = "";
            "type_id" = 8;
            vin = "";
        }
    ), ... over and over again.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:00:30+00:00Added an answer on May 28, 2026 at 7:00 pm

    With:

    [sections addObject:rows];
    [rows removeAllObjects];
    [rows addObject:dict];
    

    you keep adding the same rows object not a new one and keep changing the contents so the last contents display each time.

    Try:

    [sections addObject:[rows mutableCopy]]; 
    [rows removeAllObjects];
    [rows addObject:dict];
    

    That way a new row is added each time.

    Perhaps better yet:

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSMutableArray *sections = [NSMutableArray array];
    NSMutableArray *rows = [NSMutableArray array];
    NSString *lastDate = nil;
    for (NSDictionary *dict in [json objectForKey:@"data"]) {
        NSString *date = [dict objectForKey:@"date"];
        if ([date isEqualToString:lastDate] == NO) {
            if (lastDate == nil) {
                [rows addObject:dict];
                lastDate = date;
            } else {
                [sections addObject:rows];
                rows = [NSMutableArray array];
                [rows addObject:dict];
                lastDate = date;
            }
            [rows addObject:dict];
        }
    }
    if (rows) {
        [sections addObject:rows];
    }
    
    self.appointments = sections;
    

    Note that assigning 0 to an object does not release it, release does. But if you use convenience methods to create objects then they are autoreleased and no further action is necessary to release them.

    Also note that when comparing strings one needs to use the isEqualToString: method to compare the contents, just using the = or != only compares the address of the strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a web service that often scrapes data from remote web pages. After
Is it possible to array_push to a multidimensional array? Creating the array using: $ObjectArray
I am trying to create Jqplot bar charts and facing difficulty in creating multidimensional
I have the following multidimensional array: Array ( [0] => Array ( [id] =>
Using System, how can I dump the contents of a multidimensional array to the
I am trying to create Jqplot bar charts and facing difficulty in creating multidimensional
Is it possible to create a multidimensional, associative array in VBScript? I'm trying to
Creating Facebook event for specific page with FB Graph API There is a thread
Creating a web interface so our helpdesk can create users and setup some of
Creating my tables from my models.py . I donno how to do 2 things

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.