object1 is probably an NSArray instead of an NSMutableArray. NSArrays have fixed contents and can't be changed (mutated) after they are created. NSMutableArray is the variant that supports methods like insertObject:atIndex:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Hmmm I have object1 = [courseData objectAtIndex:arrayPosition]; where courseData is a NSMutableArray.
CourseData may be a mutable array, but it is an array of arrays, and the innermost arrays appear to be immutable. Add an NSLog statement to your code right before you try to add an object:
NSLog(@"object1 = %@", object1);
That should show the type of the array as well as it's contents. note that NSArray is a "class cluster" where the actual type of array you get is usually different than what you ask for. You should still be able to tell if it is a mutable or immutable variant.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
I figured out the issue. When I was assigning objects at the beginning of my code i used NSArray not NSMutable array. Thank you Duncan. Now I have a new problem though. The console gives me this error:
Code:
[Session started at 2010-09-10 21:01:50 -0400.]
2010-09-10 21:01:57.222 SchoolOne[20232:207] arrayPosition = 0
2010-09-10 21:02:00.178 SchoolOne[20232:207] @
2010-09-10 21:02:00.180 SchoolOne[20232:207] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5903d10
2010-09-10 21:02:00.182 SchoolOne[20232:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5903d10'
*** Call stack at first throw:
I figured out the issue. When I was assigning objects at the beginning of my code i used NSArray not NSMutable array. Thank you Duncan. Now I have a new problem though. The console gives me this error:
Code:
[Session started at 2010-09-10 21:01:50 -0400.]
2010-09-10 21:01:57.222 SchoolOne[20232:207] arrayPosition = 0
2010-09-10 21:02:00.178 SchoolOne[20232:207] @
2010-09-10 21:02:00.180 SchoolOne[20232:207] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5903d10
2010-09-10 21:02:00.182 SchoolOne[20232:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5903d10'
*** Call stack at first throw:
and its occuring in the same place.
Once again, use the debugger or an NSLog statement. It sounds like the object(s) you're trying to compare with isEqualToString is an array, not a string. Set a breakpoint just before that method call and examine the variable. Or, add an NSLog statement that displays the object, as outlined in my previous post.
Note that if you add a breakpoint, you can display the contents of variables in the console without having to resort to NSLog statements.
Use "PO" (short for print_object) to display an object. Use "p" (print) to display a scalar quantity.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
So apparently I'm not the only one who'd appreciate compiler warnings about a non-mutable object...
This is a runtime problem, not a compiler problem.
An object you get from a container object like an array is of type id (pointer of unknown type), so the compiler can't tell if it responds to a particular message.
If you want to make your code completely bulletproof you should put class checking code after extracting an object from an array, dictionary, or set.
If you create an NSArray variable directly, then the compiler WILL warn you if you try to add an object to it.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.