Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-10-2010, 07:37 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default objectAtIndex problem

Hey all,

For the life of me I cannot figure out what's wrong with my code. I keep getting the following error in the console:

Code:
[Session started at 2010-09-10 20:33:41 -0400.]
2010-09-10 20:33:48.176 SchoolOne[19977:207] arrayPosition = 0
2010-09-10 20:33:53.021 SchoolOne[19977:207] @
2010-09-10 20:33:53.022 SchoolOne[19977:207] -[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x5d543b0
2010-09-10 20:33:53.024 SchoolOne[19977:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x5d543b0'
*** Call stack at first throw:
And it's coming from this code:

Code:
- (IBAction)courseNameConfirm:(id)sender {

	
	for (int i = 0; i < 100; i++) {
		id object1 = [courseData objectAtIndex:arrayPosition];
		if ([[object1 objectAtIndex:i] isEqualToString:@""]) {
			NSLog(@"@", [object1 objectAtIndex:i]);
			[object1 insertObject:[NSArray arrayWithObject:courseNameField.text] atIndex:i];
			break;
		}
	}
	
	[self.tableView reloadData];
	
	
	[gradeSummaryNavigation dismissModalViewControllerAnimated:YES];
}
Now I know arrayPosition returns a value and so does the NSLog ( 0 and @ respectively) which means that the code is correct up until:

Code:
[object1 insertObject:[NSArray arrayWithObject:courseNameField.text] atIndex:i];
any idea?
Coolio098 is offline   Reply With Quote
Old 09-10-2010, 07:40 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Coolio098 View Post
Hey all,

For the life of me I cannot figure out what's wrong with my code. I keep getting the following error in the console:

Code:
[Session started at 2010-09-10 20:33:41 -0400.]
2010-09-10 20:33:48.176 SchoolOne[19977:207] arrayPosition = 0
2010-09-10 20:33:53.021 SchoolOne[19977:207] @
2010-09-10 20:33:53.022 SchoolOne[19977:207] -[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x5d543b0
2010-09-10 20:33:53.024 SchoolOne[19977:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x5d543b0'
*** Call stack at first throw:
And it's coming from this code:

Code:
- (IBAction)courseNameConfirm:(id)sender {

	
	for (int i = 0; i < 100; i++) {
		id object1 = [courseData objectAtIndex:arrayPosition];
		if ([[object1 objectAtIndex:i] isEqualToString:@""]) {
			NSLog(@"@", [object1 objectAtIndex:i]);
			[object1 insertObject:[NSArray arrayWithObject:courseNameField.text] atIndex:i];
			break;
		}
	}
	
	[self.tableView reloadData];
	
	
	[gradeSummaryNavigation dismissModalViewControllerAnimated:YES];
}
Now I know arrayPosition returns a value and so does the NSLog ( 0 and @ respectively) which means that the code is correct up until:

Code:
[object1 insertObject:[NSArray arrayWithObject:courseNameField.text] atIndex:i];
any idea?
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:
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-10-2010, 07:43 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

Hmmm I have object1 = [courseData objectAtIndex:arrayPosition]; where courseData is a NSMutableArray.
Coolio098 is offline   Reply With Quote
Old 09-10-2010, 07:51 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Coolio098 View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-10-2010, 08:09 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

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.
Coolio098 is offline   Reply With Quote
Old 09-10-2010, 08:14 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 71
Coolio098 is on a distinguished road
Default

Never mind i'm an idiot lol. I figured it out. Thanks for the help Duncan!
Coolio098 is offline   Reply With Quote
Old 09-10-2010, 08:14 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Coolio098 View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-10-2010, 08:26 PM   #8 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

So apparently I'm not the only one who'd appreciate compiler warnings about a non-mutable object...
__________________
If I have helped you, please consider donating. I use PayPal. It would mean a lot to me!


For more iOS Development, check out my blog—Cups of Cocoa. All visitors welcome, but especially beginners!

Hope I have helped!

Please check out IceFall, a new action game available in the App Store!
musicwind95 is offline   Reply With Quote
Old 09-10-2010, 09:12 PM   #9 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by musicwind95 View Post
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.

This code will generate a compiler warning:

Code:
NSArray* fooArray = [NSArray arrayWithObject: @"xxx"];
[fooArray addObject: @"woof"];
This code will not generate a warning, because fooArray is declared as a mutable type.

Code:
NSMutableArray* fooArray = [NSMutableArray arrayWithCapacity: 1];
[fooArray addObject: @"woof"];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 344
13 members and 331 guests
bignoggins, carlandrews, cgokey, flamingliquid, givensur, hzwegjxg, ilmman, jenniead38, linkmx, mraalex, PixelInteractive, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0