0

I'm attempting to initialize an array and add a chapter object to an array of book objects, but it crashes with the error:

2012-07-25 21:41:01.503 Project1[2364:f803] -[__NSCFString arrayOfChapters]: unrecognized selector sent to instance 0x6ad80b0 2012-07-25 21:41:01.505 Project1[2364:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString arrayOfChapters]: unrecognized selector sent to instance 0x6ad80b0'

My code:

Chapter *myChapter = [[Chapter alloc]init];
myChapter.pageCount = self.TextField2.text;
myChapter.chapterTitle = self.TextField1.text;
if(!currentBook.arrayOfChapters)
{
    currentBook.arrayOfChapters = [[NSMutableArray alloc]init];
}
currentBook = [books objectAtIndex:segControl.selectedSegmentIndex];
[currentBook.arrayOfChapters addObject:myChapter];

I think the code is correct, is there something set up wrong with my project? I believe it's the initialization which is causing the actual crash, but there isn't anything non-standard there.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Tasonir
  • 95
  • 1
  • 10
  • Where do you declare `arrayOfChapters`? – pasawaya Jul 26 '12 at 01:48
  • In the class Book: @property(nonatomic, strong) NSMutableArray *arrayOfChapters; – Tasonir Jul 26 '12 at 01:49
  • 1
    Do you initialize `currentBook` anywhere before the line `currentBook = [books objectAtIndex:segControl.selectedSegmentIndex];`? – Ethan Holshouser Jul 26 '12 at 01:53
  • currentBook is a local variable which is initialized in the line of code on screen - which is just after I check if it's null. I've moved that line of code above the if statement, but it still crashes with the same error. – Tasonir Jul 26 '12 at 01:53
  • You can't refer to a property (`arrayOfChapters`) of an item that's not, itself, initialized. You want to check `currentBook` before you start using it. – Rob Jul 26 '12 at 01:54
  • You aren't checking if `currentBook` is nil, you're checking if `currentBook.arrayOfChapters` returns nil, which is not the same thing. If moving that line above the if statement doesn't fix the problem, then there is likely something wrong with `books`. What is `books` (I'm assuming it's an `NSArray`?) and where is it initialized? – Ethan Holshouser Jul 26 '12 at 01:59
  • Books is an NSMutableArray which holds chapters, and chapters contain a title and page count. I added an if(!currentBook) check, and currentBook is non-null. Is it possible to get in trouble if I'm accidentally double allocing it? – Tasonir Jul 26 '12 at 02:03
  • Possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](https://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Cœur Jul 08 '19 at 05:55

3 Answers3

1

It looks like currentBook is a string or not initialized.

Dmorneault
  • 199
  • 1
  • 8
  • Turns out it was a string - I had originally had a more simple data structure before I added more variables to chapter/book and hadn't gone back and actually created a book where I thought I had one... – Tasonir Jul 26 '12 at 02:26
1

you can make a breakpoint on the line "if(!currentBook.arrayOfChapters)" to check whether the currentBook is nil;

make a breakpoint on the line"currentBook = [books objectAtIndex:segControl.selectedSegmentIndex]; " to check whether the segControl.selectedSegmentIndex >= [books count]

jack del
  • 36
  • 2
0

I may be misreading this but...

    if(!currentBook.arrayOfChapters)
    {
        // creating array for current book
        currentBook.arrayOfChapters = [[NSMutableArray alloc]init]; 
    }

    // reassigning a new book to current book. Are you sure this new one has array of chapters?
    currentBook = [books objectAtIndex:segControl.selectedSegmentIndex]; 

    // trying to access array of chapters
    [currentBook.arrayOfChapters addObject:myChapter]; 

Shouldn't you be assigning first and then creating array if it's not already there?

Kirby Todd
  • 11,254
  • 3
  • 32
  • 60
  • Yes, I should, and I did make that change, although that wasn't what caused the crash. CurrentBook was completely lying, it was actually a string which I set entirely incorrectly elsewhere. – Tasonir Jul 26 '12 at 02:38