title: Strange NSOrderedSet Behavior
body: <p>I have this code which should properly set up a table view with calendar events:</p>
  
  <pre><code>...
  
  #pragma mark - UITableView Delegate and DataSource methods
  
  - (int)numberOfSectionsInTableView:(UITableView *)tableView {
  
      ...
  
      NSLog(@"TEST %d", self.user.calendarDays.count);
      NSLog(@"TEST %@", [self.user.calendarDays objectAtIndex:3]);
      return self.user.calendarDays.count;
  
      ...
  
  }
  
  ...    
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
  
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
          cell.textLabel.font = [UIFont systemFontOfSize:15];
      }
  
      ...
  
      NSOrderedSet *calendarDays = self.user.calendarDays;
      NSLog(@"CalendarDays: [%@]", calendarDays);
      CalendarDay *calendarDay = [calendarDays objectAtIndex:indexPath.section];
      NSLog(@"CalendarDay: [%@]", calendarDay);
      cell.textLabel.text = [[[calendarDay calendarEvents] objectAtIndex:indexPath.row] title];
  
      ...
  
      return cell;
  }
  
  ...
  </code></pre>
  
  <p>However the app crashes every time on the line <code>CalendarDay *calendarDay = [calendarDays objectAtIndex:indexPath.section];</code>. The output (including NSLogs):</p>
  
  <pre><code>2013-11-25 13:05:34.731 Nobles[9176:60b] TEST 4
  2013-11-25 13:05:34.734 Nobles[9176:60b] TEST &lt;CalendarDay: 0x155a3960&gt; (entity: CalendarDay; id: 0x155a2f50 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/CalendarDay/p26&gt; ; data: &lt;fault&gt;)
  2013-11-25 13:05:35.280 Nobles[9176:60b] CalendarDays: [Relationship 'calendarDays' on managed object (0x155616e0) &lt;User: 0x155616e0&gt; (entity: User; id: 0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt; ; data: {
      assemblySeat = "";
      calendarDays =     (
          "0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt;",
          "0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt;",
          "0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt;"
      );
      correctPIN = 1;
      cubby = 1;
      faculty = 0;
      homeSportEvents = "&lt;relationship fault: 0x155ae7d0 'homeSportEvents'&gt;";
      lockerCombo = "";
      lockerNum = "";
      macLockerCombo = "3-29-35";
      macLockerNum = 133;
      menuBackground = 0;
      mySportEvents = "&lt;relationship fault: 0x155ae820 'mySportEvents'&gt;";
      name = Carlo;
      parent = 0;
      peopleID = 21894;
      pin = cabelli16;
      proPic = &lt;ffd8ffe0 00104a46 49460001 000100fa 00fa0000 fffe001f 4c454144 20546563 686e6f6c 6f676965 7320496e 632e2056 312e&gt;;
      reservations = "&lt;relationship fault: 0x155ae860 'reservations'&gt;";
      resultSportEvents = "&lt;relationship fault: 0x155ae8a0 'resultSportEvents'&gt;";
      scheduleDays = "&lt;relationship fault: 0x155ae8e0 'scheduleDays'&gt;";
      student = 1;
      unid = cabelli16;
  }) with objects {(
      &lt;CalendarDay: 0x1567ab70&gt; (entity: CalendarDay; id: 0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt; ; data: {
      calendarEvents =     (
          "0x155b1820 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A2&gt;"
      );
      dateString = "11/16/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  }),
      &lt;CalendarDay: 0x15683c80&gt; (entity: CalendarDay; id: 0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt; ; data: {
      calendarEvents =     (
          "0x15683940 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A4&gt;"
      );
      dateString = "11/17/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  }),
      &lt;CalendarDay: 0x156842d0&gt; (entity: CalendarDay; id: 0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt; ; data: {
      calendarEvents =     (
          "0x15684150 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A6&gt;"
      );
      dateString = "11/18/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  })
  )}]
  2013-11-25 13:05:48.933 Nobles[9176:60b] CalendarDay: [&lt;CalendarDay: 0x1567ab70&gt; (entity: CalendarDay; id: 0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt; ; data: {
      calendarEvents =     (
          "0x155b1820 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A2&gt;"
      );
      dateString = "11/16/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  })]
  2013-11-25 13:05:48.940 Nobles[9176:60b] CalendarDays: [Relationship 'calendarDays' on managed object (0x155616e0) &lt;User: 0x155616e0&gt; (entity: User; id: 0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt; ; data: {
      assemblySeat = "";
      calendarDays =     (
          "0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt;",
          "0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt;",
          "0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt;"
      );
      correctPIN = 1;
      cubby = 1;
      faculty = 0;
      homeSportEvents = "&lt;relationship fault: 0x155ae7d0 'homeSportEvents'&gt;";
      lockerCombo = "";
      lockerNum = "";
      macLockerCombo = "3-29-35";
      macLockerNum = 133;
      menuBackground = 0;
      mySportEvents = "&lt;relationship fault: 0x155ae820 'mySportEvents'&gt;";
      name = Carlo;
      parent = 0;
      peopleID = 21894;
      pin = cabelli16;
      proPic = &lt;ffd8ffe0 00104a46 49460001 000100fa 00fa0000 fffe001f 4c454144 20546563 686e6f6c 6f676965 7320496e 632e2056 312e&gt;;
      reservations = "&lt;relationship fault: 0x155ae860 'reservations'&gt;";
      resultSportEvents = "&lt;relationship fault: 0x155ae8a0 'resultSportEvents'&gt;";
      scheduleDays = "&lt;relationship fault: 0x155ae8e0 'scheduleDays'&gt;";
      student = 1;
      unid = cabelli16;
  }) with objects {(
      &lt;CalendarDay: 0x1567ab70&gt; (entity: CalendarDay; id: 0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt; ; data: {
      calendarEvents =     (
          "0x155b1820 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A2&gt;"
      );
      dateString = "11/16/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  }),
      &lt;CalendarDay: 0x15683c80&gt; (entity: CalendarDay; id: 0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt; ; data: {
      calendarEvents =     (
          "0x15683940 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A4&gt;"
  
      );
      dateString = "11/17/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  }),
      &lt;CalendarDay: 0x156842d0&gt; (entity: CalendarDay; id: 0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt; ; data: {
      calendarEvents =     (
          "0x15684150 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A6&gt;"
      );
      dateString = "11/18/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  })
  )}]
  2013-11-25 13:05:50.116 Nobles[9176:60b] CalendarDay: [&lt;CalendarDay: 0x15683c80&gt; (entity: CalendarDay; id: 0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt; ; data: {
      calendarEvents =     (
          "0x15683940 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A4&gt;"
      );
      dateString = "11/17/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  })]
  2013-11-25 13:05:50.122 Nobles[9176:60b] CalendarDays: [Relationship 'calendarDays' on managed object (0x155616e0) &lt;User: 0x155616e0&gt; (entity: User; id: 0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt; ; data: {
      assemblySeat = "";
      calendarDays =     (
          "0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt;",
          "0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt;",
          "0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt;"
      );
      correctPIN = 1;
      cubby = 1;
      faculty = 0;
      homeSportEvents = "&lt;relationship fault: 0x155ae7d0 'homeSportEvents'&gt;";
      lockerCombo = "";
      lockerNum = "";
      macLockerCombo = "3-29-35";
      macLockerNum = 133;
      menuBackground = 0;
      mySportEvents = "&lt;relationship fault: 0x155ae820 'mySportEvents'&gt;";
      name = Carlo;
      parent = 0;
      peopleID = 21894;
      pin = cabelli16;
      proPic = &lt;ffd8ffe0 00104a46 49460001 000100fa 00fa0000 fffe001f 4c454144 20546563 686e6f6c 6f676965 7320496e 632e2056 312e&gt;;
      reservations = "&lt;relationship fault: 0x155ae860 'reservations'&gt;";
      resultSportEvents = "&lt;relationship fault: 0x155ae8a0 'resultSportEvents'&gt;";
      scheduleDays = "&lt;relationship fault: 0x155ae8e0 'scheduleDays'&gt;";
      student = 1;
      unid = cabelli16;
  }) with objects {(
      &lt;CalendarDay: 0x1567ab70&gt; (entity: CalendarDay; id: 0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt; ; data: {
      calendarEvents =     (
          "0x155b1820 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A2&gt;"
      );
      dateString = "11/16/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  }),
      &lt;CalendarDay: 0x15683c80&gt; (entity: CalendarDay; id: 0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt; ; data: {
      calendarEvents =     (
          "0x15683940 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A4&gt;"
      );
      dateString = "11/17/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  }),
      &lt;CalendarDay: 0x156842d0&gt; (entity: CalendarDay; id: 0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt; ; data: {
      calendarEvents =     (
          "0x15684150 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A6&gt;"
      );
      dateString = "11/18/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  })
  )}]
  2013-11-25 13:05:51.548 Nobles[9176:60b] CalendarDay: [&lt;CalendarDay: 0x156842d0&gt; (entity: CalendarDay; id: 0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt; ; data: {
      calendarEvents =     (
          "0x15684150 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A6&gt;"
      );
      dateString = "11/18/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  })]
  2013-11-25 13:05:51.553 Nobles[9176:60b] CalendarDays: [Relationship 'calendarDays' on managed object (0x155616e0) &lt;User: 0x155616e0&gt; (entity: User; id: 0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt; ; data: {
      assemblySeat = "";
      calendarDays =     (
          "0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt;",
          "0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt;",
          "0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt;"
      );
      correctPIN = 1;
      cubby = 1;
      faculty = 0;
      homeSportEvents = "&lt;relationship fault: 0x155ae7d0 'homeSportEvents'&gt;";
      lockerCombo = "";
      lockerNum = "";
      macLockerCombo = "3-29-35";
      macLockerNum = 133;
      menuBackground = 0;
      mySportEvents = "&lt;relationship fault: 0x155ae820 'mySportEvents'&gt;";
      name = Carlo;
      parent = 0;
      peopleID = 21894;
      pin = cabelli16;
      proPic = &lt;ffd8ffe0 00104a46 49460001 000100fa 00fa0000 fffe001f 4c454144 20546563 686e6f6c 6f676965 7320496e 632e2056 312e&gt;;
      reservations = "&lt;relationship fault: 0x155ae860 'reservations'&gt;";
      resultSportEvents = "&lt;relationship fault: 0x155ae8a0 'resultSportEvents'&gt;";
      scheduleDays = "&lt;relationship fault: 0x155ae8e0 'scheduleDays'&gt;";
      student = 1;
      unid = cabelli16;
  }) with objects {(
      &lt;CalendarDay: 0x1567ab70&gt; (entity: CalendarDay; id: 0x1567ade0 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A3&gt; ; data: {
      calendarEvents =     (
          "0x155b1820 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A2&gt;"
      );
      dateString = "11/16/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  }),
      &lt;CalendarDay: 0x15683c80&gt; (entity: CalendarDay; id: 0x15683d00 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A5&gt; ; data: {
      calendarEvents =     (
          "0x15683940 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A4&gt;"
  
      );
      dateString = "11/17/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  }),
      &lt;CalendarDay: 0x156842d0&gt; (entity: CalendarDay; id: 0x15684470 &lt;x-coredata:///CalendarDay/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A7&gt; ; data: {
      calendarEvents =     (
          "0x15684150 &lt;x-coredata:///CalendarEvent/t2757F3FB-BC75-435C-B0A4-FDA936D3E91A6&gt;"
      );
      dateString = "11/18/2013";
      user = "0x1554ad10 &lt;x-coredata://FA2FBE99-00BF-4603-8479-9DAC1881AAC6/User/p1&gt;";
  })
  )}]
  2013-11-25 13:05:53.801 Nobles[9176:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSOrderedSetM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
  *** First throw call stack:
  (0x3099ae83 0x3acf76c7 0x3092ed87 0xd17fb 0x33251315 0x331f96cd 0x331f8ef1 0x3311f353 0x32da5943 0x32da1167 0x32da0ff9 0x32da0a0d 0x32da081f 0x3311768b 0x30965f69 0x309638f7 0x30963c43 0x308ce471 0x308ce253 0x356082eb 0x33183845 0xbff3d 0x3b1f0ab7)
  libc++abi.dylib: terminating with uncaught exception of type NSException
  </code></pre>
  
  <p>Why can the objectAtIndex:3 be retrived in the NSLog in the numberOfSections method but not in the cellForRow method?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I have no idea if this is related or not but a few days back (when my Calendar was still sort of working) I posted this question about something strange going on: <a href="https://stackoverflow.com/questions/20183013/uitableview-search-causes-strange-scrolling">UITableView Search Causes Strange Scrolling</a></p>