Re: MutableDictionary not adding objects?
Re: MutableDictionary not adding objects?
- Subject: Re: MutableDictionary not adding objects?
- From: Jonathan Jackel <email@hidden>
- Date: Mon, 26 Jan 2004 10:46:29 -0500
Just to elaborate on mmalcolm's answer, tempArray always points to the
same mutable array throughout your method. When you add tempArray to
another object (ABCMenusArray, for instance), you are actually adding a
reference to tempArray, not a copy of tempArray. If tempArray changes,
so do the contents of the other object, which merely reflect whatever
is in tempArray at any given moment.
There are basically two solutions.
1. Create a new tempArray every time you need one.
2. Reuse tempArray, but store copies in the other object, like so:
[ABCMenusArray addObject;[[tempArray mutableCopy] autorelease]];
The copy is your responsibility, memory-management-wise, so you need to
autorelease it.
Option 1 is probably the more natural choice here because you remove
all objects from the array at the end of each block. With option 1,
this becomes unnecessary because you start with a fresh, empty array
each time.
Jonathan
On Jan 25, 2004, at 7:16 PM, email@hidden wrote:
ok so i have this code to cycle through an array check the values add
them to other dictionaries and arrays then output it to a file the
problem is the tempDict's being added are empty when i open the xml
file output? does anything look wrong with this code?
NSEnumerator *e = [fieldsArray objectEnumerator];
id object;
NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
NSMutableArray *tempArray = [NSMutableArray array];
while(object = [e nextObject])
{
if ([object isEqualToString:@":"])
{
[ABCMenusArray addObject:tempArray];
[tempArray removeAllObjects];
}
else if ([object isEqualToString:@"sep"])
{
[tempDict setObject:@"sep" forKey:@"sep"];
[tempArray addObject:tempDict];
[tempDict removeAllObjects];
}
else
{
[tempDict setObject:object forKey:@"item"];
[tempDict setObject:[NSNumber numberWithBool:NO]
forKey:@"statsus"];
[tempArray addObject:tempDict];
[tempDict removeAllObjects];
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.