Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.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-04-2008, 05:03 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 401
Default NSMutableArray "replace object at index" problem

Hello,

In my latest project I'm using an NSMutableArray, but when I try to replace an object the app crashes (TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION).

Code:
- (void)ThisFunctionReplacesAnObject {
    [nameOfMyArray replaceObjectAtIndex:0 with Object: @"newObject.png"];
}
The NSMutableArray is declared in the .h file and released in .m (dealloc method).

What am I doing wrong here?

Thanks in advance
NewiPhoneDeveloper is offline   Reply With Quote
Old 09-04-2008, 09:14 PM   #2 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 7
Default

There are two documented exceptions thrown by replaceObjectAtIndex. One results from withObject being nil; in your case that is unlikely. The other is that the index exceeds the bounds of the array: are you sure there is at least one pre-existing object in the array?
iDevGuy is offline   Reply With Quote
Old 09-04-2008, 11:06 PM   #3 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
Default

To go along with what iDevGuy said, are you sure that you've alloc'ed and init'ed the NSMutableArray?
myersn024 is offline   Reply With Quote
Old 09-05-2008, 04:39 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 401
Default

Thanks for your answers...

I created the NSMutableArray as follows:

Code:
myNSMutableArray = [[NSMutableArray alloc] init with objects:@"object1.png", @"object2.png", "object3.png"];
"myNSMutableArray is also declared in the .h file, like:

Code:
NSMutableArray *myNSMutableArray;

//below @interface

@property (nonatomic, retain) NSMutableArray *myNSMutableArray;
I guess I'm just doing a stupid mistake here...
NewiPhoneDeveloper is offline   Reply With Quote
Old 09-05-2008, 05:14 AM   #5 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 8
Default

I have this problem before in my code, and let me guess, you have created the array in your "init" or "initWith...." method, right?

To properly create a property with code (that is, not a UI control from Interface Builder) in "init" method, *always* retain your property.

In short,

myNSMutableArray = [[NSMutableArray alloc] initWith....];

should be

myNSMutableArray = [[[NSMutableArray alloc] initWith....] retain];

this way, even your "init" method ends, the retain count of "myNSMutableArray" will prevent the system to free/release your object.

Alternatively, since you declare the property in (retain) way, you can use

self.myNSMutableArray = [[NSMutableArray alloc] initWith...];

Using accessor will do the retain for you.
ckcheng is offline   Reply With Quote
Old 09-05-2008, 05:26 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 401
Default

Hi,

I tried what you suggested and it really makes sense to me, but still "TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION".

Here is what I did:

Code:
//this is MainViewController.m (NSMutable array is already declared in .h)

myNSMutableArray = [[[NSMutableArray alloc] initWithObjects:@"object1.png", @"object2.png", @"object3.png"]retain];

- (void)ReplaceOneOfMyObjects {
    [myNSMutableArray replaceObjectAtIndex:0 withObject:@"object4.png"];
}

[self ReplaceOneOfMyObjects];
NewiPhoneDeveloper is offline   Reply With Quote
Old 09-05-2008, 05:56 AM   #7 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 8
Default

It sounds strange.

Have you tried print the count and the content of the array in your ReplaceOneOfMyObjects method before doing the replaceObjectAtIndex:withObject: ?

Code:
NSLog([NSString stringWithFormat:@"my array count = %d, content = %@", [myMutableArray count], myMutableArray]);
ckcheng is offline   Reply With Quote
Old 09-05-2008, 08:30 AM   #8 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 7
Default

Quote:
Originally Posted by NewiPhoneDeveloper View Post
Hi,

I tried what you suggested and it really makes sense to me, but still "TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION".

Here is what I did:

Code:
//this is MainViewController.m (NSMutable array is already declared in .h)

myNSMutableArray = [[[NSMutableArray alloc] initWithObjects:@"object1.png", @"object2.png", @"object3.png"]retain];

- (void)ReplaceOneOfMyObjects {
    [myNSMutableArray replaceObjectAtIndex:0 withObject:@"object4.png"];
}

[self ReplaceOneOfMyObjects];
"nil" is missing:

Code:
myNSMutableArray = [[NSMutableArray alloc] initWithObjects: @"object1.png", @"object2.png", @"object3.png", nil];
cleverapps is offline   Reply With Quote
Old 09-05-2008, 08:33 AM   #9 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 401
Default

Sorry, forgot nil in my post.

Anyway, I will investigate now every step in my app and report back.

Thanks for all your help!
NewiPhoneDeveloper is offline   Reply With Quote
Old 09-06-2008, 07:56 AM   #10 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 91
Default

Quote:
Originally Posted by ckcheng View Post
To properly create a property with code (that is, not a UI control from Interface Builder) in "init" method, *always* retain your property.

myNSMutableArray = [[NSMutableArray alloc] initWith....];
should be
myNSMutableArray = [[[NSMutableArray alloc] initWith....] retain];
This is totally incorrect, alloc automatically retains as do methods with new or copy in them. The connivence class methods return a retained and autoreleased item and need to be retained it need be.
danzaph is offline   Reply With Quote
Old 09-06-2008, 08:28 AM   #11 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 91
Default

1) You need a @synthesize call in the implementation.
Code:
@synthesize myNSMutableArray;
2) use "self." to use the setter/getters produced by the parameters.
Code:
	self.myNSMutableArray = [[NSMutableArray alloc] initWithObjects:@"object1.png", @"object2.png", @"object3.png", nil];
	[self.myNSMutableArray replaceObjectAtIndex:0 withObject: @"newObject.png"];
Without the "self." you are changing the iVar directly.
danzaph is offline   Reply With Quote
Old 09-06-2008, 08:59 AM   #12 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

When you see the TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION message there is more info in the Xcode Debugger Console. Open up the console window and look for the kind of exception. As mentioned upthread there are different kinds of exceptions. Looking at the kind of exception will help you to figure out the problem. You can also see the NSLog output in Console.app.

Also, by the time the app is terminated due to the uncaught exception there is no useful backtrace. If you set a breakpoint on objc_exception_throw the debugger will break before the exception is thrown and you'll have a useful backtrace. I do this with a .gdbinit file. Create a file named .gdbinit and place it in your home directory. This is the contents of mine:

fb -[NSException raise]
fb -[_NSZombie release]
fb szone_error
fb objc_exception_throw

It's also possible to set these kinds of breakpoints in the Xcode breakpoints window or in the debugger console.

At any rate, the usual reason for an exception with replaceObjectAtIndex is out of range. Another reason is that your instance variable has already been released due to faulty memory management so you message a stale pointer.
PhoneyDeveloper is offline   Reply With Quote
Old 10-02-2008, 03:01 PM   #13 (permalink)
New Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 74
Default

what if you want to replace one instance IN the object?

if object1 at index 0 has 2 keys: firstname lastname;

how would I replace the lastname Without touching the firstname? can you get that granular with an array? or do you have to write the entire array every change? doesnt this create a lot of overhead?

cheers.bo
roberthuttinger is offline   Reply With Quote
Old 04-29-2009, 12:58 PM   #14 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 27
Default

Quote:
Originally Posted by ckcheng View Post
I have this problem before in my code, and let me guess, you have created the array in your "init" or "initWith...." method, right?

To properly create a property with code (that is, not a UI control from Interface Builder) in "init" method, *always* retain your property.

In short,

myNSMutableArray = [[NSMutableArray alloc] initWith....];

should be

myNSMutableArray = [[[NSMutableArray alloc] initWith....] retain];

this way, even your "init" method ends, the retain count of "myNSMutableArray" will prevent the system to free/release your object.

Alternatively, since you declare the property in (retain) way, you can use

self.myNSMutableArray = [[NSMutableArray alloc] initWith...];

Using accessor will do the retain for you.


thank you for this post. I would've been there all day. retain, retain, retain!
remover 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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,282
Threads: 39,072
Posts: 171,339
Top Poster: smasher (2,575)
Welcome to our newest member, FANFAN
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:47 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0