Re: More on dictionaries
Re: More on dictionaries
- Subject: Re: More on dictionaries
- From: "Sailesh Agrawal" <email@hidden>
- Date: Wed, 15 Oct 2003 17:01:46 -0400
I'm not sure I totally understand the question but I'll give a shot at
it. If your question is "once I've found the key where it's stored how
do I delete it so that it's removed from the master dictionary?" then the
answer is "just remove it from the dictionary you're at".
If you need to know how to find it in the first place try something like
this:
[self removeObject:objectToDelete fromDict:masterDict];
where removeObject is defined as:
-(id)removeObject:(id)theObject fromDict:(NSMutableDictionary *)theDict
{
NSEnumerator *enumerator = [theDict keyEnumerator];
id key, rval, object;
// search this dictionary for the object
while (key = [enumerator nextObject])
{
if ([theDict objectForKey:key] == theObject)
{
// we found it
// now delete it from the master dictionary
[theDict removeObjectForKey:key];
return theObject;
}
}
// reacursively serach
enumerator = [theDict objectEnumerator];
while (object = [enumerator nextObject])
{
// only recurse on dictionaries
if ([object class] == [NSMutableDictionary class])
{
rval = [self removeObject:theObject fromDict:object];
if (rval)
return rval;
}
}
// couldn't find it
return nil;
}
good luck,
Sailesh
On Tue, 14 Oct 2003 23:52:15 -0700, "April Gendill"
<email@hidden> said:
>
I have a dictionary structure that is basically built from a directory
>
structure. I need to be able to search this dictionary as deep as it
>
goes (files are strings and directories are nested dictionaries in
>
this structure)
>
here's what information I have when I begin the search:
>
I know the root dictionary. Since the dictionary is a representation
>
of a directory structure you could think of it as /root/item/....
>
I know the key name for the item that must be move/deleted
>
It's pretty easy to find the item. I run a loop.
>
in the loop I have:
>
if([theDictionary objectForKey:theSearchItem]){
>
do something then leave the loop
>
}
>
The problem is the do something. I have to be able to remove the
>
object from the dictionary and I have no idea how. since the item to
>
remove can be many parent/children deep I don't know how to remove it
>
from the master dictionary..
>
>
I know that I'll need to return an id since I could either be dealing
>
with a string or dictionary.
>
I've been trying to pass it as [self removeObject:theObject
>
fromDict:masterDict]
>
the function is written as: -(id)removeObject:(id)theObject
>
fromDict:(NSMutableDictionary *)theDict
>
>
I used a mutable dictionary so I could edit it as I went, but thats
>
the problem... I'm not sure how to remove the item when I find it and
>
then put the master dictionary back together correctly.
>
_______________________________________________
>
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.