Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Can't add object to NSMutableArray: help!

Can't add object to NSMutableArray: help!
Thread Tools
kman42
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 9, 2002, 10:13 AM
 
For some reason I can't add an object to an NSMutableArray as follows:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSLog(<font color = orange>@"referenceDictionary=%@"</font>,[referenceDictionary allKeys]);
while ([idScanner isAtEnd]==NO) {
NSLog(<font color = orange>@"scanning new refs"</font>);
[idScanner scanUpToString:<font color = orange>@" "</font> intoString:&idInstance];
NSLog(<font color = orange>@"idInstance=%@"</font>,idInstance);

if ([[referenceDictionary allKeys] containsObject:idInstance] !=YES) {
NSLog(<font color = orange>@"contains=no"</font>);
[newReferenceArray addObject:idInstance];
}

}
NSLog(<font color = orange>@"newrefarray=%@"</font>,newReferenceArray);

</font>[/code]
The idInstance is fine and is output with NSLog. I get the output "contains=no". The problem is that the newReferenceArray outputs (null) in the last NSLog statement. newReferenceArray was allocated previously with
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
[self setNewReferenceArray:[NSMutableArray array]];
</font>[/code]

Any idea what's going on here?

Thanks,
kman
     
lfrog2
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 9, 2002, 11:55 AM
 
Did you retain newReferenceArray? I am not quite sure what you are doing in setNewReferenceArray:. What is done with the [NSMutableArray array] that is passed to setNewReferenceArray:?
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 9, 2002, 12:24 PM
 
Here are the newReferenceArray accessor methods:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
-(NSMutableArray *)newReferenceArray {
return newReferenceArray;
}
-(void) setNewReferenceArray NSMutableArray *)array {
[array retain];
[newReferenceArray release];
newReferenceArray=array;
}

</font>[/code]

newReferenceArray is declared in the class header. It's first and only use prior to the addObject method in the code block of my first post is it's allocation in the init method of the class:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
-(id) init
{
if (self =[super init]) {
[self setReferenceDictionary:[NSMutableDictionary dictionary]];
[self setNewReferenceArray:[NSMutableArray array]];
[self setNumberOfNewReferences:[NSNumber numberWithInt: <font color = blue>0</font>]];

}
return self;
}
</font>[/code]

kman
     
Ghoser777
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
May 9, 2002, 07:18 PM
 
Puts some NSLog's in the other two methods you just posted... I've had issues with +array in the past.

F-bacher
     
lindberg
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 11, 2002, 05:44 AM
 
I would put logs in the other methods to make sure they're being called when you think they are. It's either never getting set, or it's being nilled out by something else. I can't imagine +array having any issues though.

You also might try cleaning and rebuilding from scratch. There may be a compiled class that has its ivar offsets wrong or something, which can lead to all sorts of mysterious stuff.

Another thing that sometimes happens is that if you have an outlet named "foo", and a -setFoo: method, that method will be called to set the outlet when the nib is loaded instead of the ivar being set directly. I don't think that's the case here though.
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 11, 2002, 11:27 AM
 
Thanks for the help. I was actually trying to debug some code that acted on stuff that was being unarchived from a file. The init routine was not run in this case so even though I changed the init code to alloc the array, it was never being allocated. Once I deleted the archive file and started from scratch, all my changes took effect since my objects were now being created from scratch and the init routines were being run.

kman
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 12:36 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,