Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized Selector Sent to Instance [NSCFString subarrayWithRange:]

I have the following code which is producing this error. I cannot understand why the subarrayWithRange message is being sent to a string? When it is clearly an array?

static const int kItemsPerView = 20;
NSRange rangeForView = NSMakeRange( page * kItemsPerView, kItemsPerView );

NSMutableArray *temp = [[APP_DELEGATE keysArray] mutableCopyWithZone:NULL]; 
NSArray *itemsForView = [temp subarrayWithRange:rangeForView];

for (int loopCounter = 0;loopCounter < r*c;loopCounter++){
    NSLog(@"%i: %@ ", loopCounter, [itemsForView objectAtIndex:loopCounter]);
}

Error:

-[NSCFString subarrayWithRange:]: unrecognized selector sent to instance 0x6b071a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: [NSCFString subarrayWithRange:]:

Thanks

like image 811
joec Avatar asked Oct 04 '10 19:10

joec


2 Answers

These kinds of errors are usually memory-management-related. Essentially, you're sending a message to an address that's now occupied by some other object because the previous occupant has unexpectedly disappeared. Since that address space could be occupied by anything, you just happen to be asking an NSCFString something to which it doesn't respond.

If you pause the debugger right after you create the temp array, what do you see assigned to temp? I'm guessing something's not quite right with whatever -keysArray returns. You might want to double-check how the memory is handled in whatever that's supposed to return. By the name, I suppose your app delegate has an array called "keysArray" as an instance variable. Perhaps that's not being properly retained when it's created or assigned?

like image 162
Joshua Nozzi Avatar answered Oct 13 '22 20:10

Joshua Nozzi


So I had this one. I did something stupid. I assigned the UITextView to a string instead of it's text property. ie:

myObj.txtbxThing = [NSString stringWithFormat:@"%@", stuffString];

instead of:

myObj.txtbxThing.text = [NSString stringWithFormat:@"%@", stuffString];
like image 43
Jeff Avatar answered Oct 13 '22 18:10

Jeff