 |
 |
|
 |
09-04-2008, 05:03 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Posts: 401
|
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
|
|
|
09-04-2008, 09:14 PM
|
#2 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 7
|
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?
|
|
|
09-04-2008, 11:06 PM
|
#3 (permalink)
|
|
Tutorial Author
Join Date: May 2008
Posts: 315
|
To go along with what iDevGuy said, are you sure that you've alloc'ed and init'ed the NSMutableArray?
|
|
|
09-05-2008, 04:39 AM
|
#4 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Posts: 401
|
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...
|
|
|
09-05-2008, 05:14 AM
|
#5 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 8
|
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.
|
|
|
09-05-2008, 05:26 AM
|
#6 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Posts: 401
|
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];
|
|
|
09-05-2008, 05:56 AM
|
#7 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 8
|
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]);
|
|
|
09-05-2008, 08:30 AM
|
#8 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 7
|
Quote:
Originally Posted by NewiPhoneDeveloper
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];
|
|
|
09-05-2008, 08:33 AM
|
#9 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Posts: 401
|
Sorry, forgot nil in my post.
Anyway, I will investigate now every step in my app and report back.
Thanks for all your help!
|
|
|
09-06-2008, 07:56 AM
|
#10 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 91
|
Quote:
Originally Posted by ckcheng
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.
|
|
|
09-06-2008, 08:28 AM
|
#11 (permalink)
|
|
New Member
Join Date: Jul 2008
Posts: 91
|
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.
|
|
|
09-06-2008, 08:59 AM
|
#12 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 1,431
|
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.
|
|
|
10-02-2008, 03:01 PM
|
#13 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 74
|
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
|
|
|
04-29-2009, 12:58 PM
|
#14 (permalink)
|
|
Registered Member
Join Date: Apr 2009
Posts: 27
|
Quote:
Originally Posted by ckcheng
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!
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 257 |
| 19 members and 238 guests |
| bluemonkey, chk79ae, davide.k, dbonneville, FANFAN, fredidf, giraffe81, iruirc, Jompe71, learnSomething, nico.overbeeke, oscar.peli, robertyang999, Rossco, Saurman, schoash, SimonHodgkiss, waste str, yahya.sadiiq |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,282
Threads: 39,072
Posts: 171,339
Top Poster: smasher (2,575)
|
| Welcome to our newest member, FANFAN |
|