- (void)reorderPartners { if (!self.cardsArray || !self.cardsArray.count) { return; } NSMutableArray *newArray = [NSMutableArray array]; NSMutableArray *oldArray = [self.cardsArray mutableCopy]; int partnersToFind = 1; int index = 0; while (partnersToFind) { NSString* brand = ((Card*)oldArray[index]).company.brand; if ([brand isEqualToString:@"smth"]) { [newArray addObject:oldArray[index]]; [oldArray removeObject:oldArray[index]]; partnersToFind--; index--; } index++; } partnersToFind = 2; index = 0; while (partnersToFind) { NSString *brand = ((Card*)oldArray[index]).company.brand; if ([brand isEqualToString:@"smth"] || [brand isEqualToString:@"smth"]) { [newArray addObject:oldArray[index]]; [oldArray removeObject:oldArray[index]]; NSLog(@"%lu", (unsigned long)oldArray.count); partnersToFind--; index--; } index++; } for (Card* card in oldArray) { NSString* brand = card.company.brand; if (![brand isEqualToString:@"smth"]) { [newArray addObject:card]; } } self.cardsArray = [newArray copy]; 

} At assembly such error arrives: - [__ NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array '. I understand that this is due to an empty array. But after 10 hours of debugging, I’m thinking a little. Do not kick much, help me figure it out.

  • Make a check before taking values ​​from an empty array. - Orest Mykha
  • @OrestMykha, do you mean a code like if array.isEmpty (...) then take values? - Vladislav Hodeev
  • @OrestMykha, got to the bottom. It turned out that the array is empty because it refers to another array. Which, in turn, refers to an entity in Core Data. What eloquently says: "<NSManagedObject: 0x7a7b1cf0> (entity: Card; id: 0x7b970340 <x-coredata: // C2D5D0A5-2D4E-48FB-B0B4-F8C928BFD2AA / Card / p4>;); scrape)) Thank you for your valuable advice! - Vladislav Hodeev
  • Glad to help, Good luck! - Orest Mykha

0