Tasonir
Tasonir

Reputation: 95

unrecognized selector sent to instance

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.

Upvotes: 0

Views: 745

Answers (3)

jack del
jack del

Reputation: 36

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]

Upvotes: 1

Kirby Todd
Kirby Todd

Reputation: 11566

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?

Upvotes: 0

Dmorneault
Dmorneault

Reputation: 199

It looks like currentBook is a string or not initialized.

Upvotes: 1

Related Questions