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 > Enumerate

Enumerate
Thread Tools
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 2, 2002, 09:06 AM
 
I have a mental block, please help me

This is an Obj-C question:
I have a dictionary and I want to make an ARRAY with all keys and their objects.

It's probably something very simple like arrayFromDictionary: but my brain just melt down
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 2, 2002, 10:56 AM
 
Basically, I am trying to do something like a loop that does the following:

[myArray setObject:<first item in myDictionary>];
[tableView reloadData];
[myArray setObject:<next item>];

etc.

It should be possible using enumerator or something but I can't find a good example.

Anyone?
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Sep 2, 2002, 01:15 PM
 
An array with all the keys and objects? Uhmm... array's hold one object each index, so you'll need to make some custom subclass that has instance variables of an object and a key.

Code:
#import <Cocoa/Cocoa.h> @interface ObjectKeyHolder : NSObject { id object; id key; } - (id)initWithObject:(id)obj key:(id)k; - (id)object; - (id)key; @end
Then you could do this:

Code:
NSDictionary *myDict; NSArray *allKeys = [myDict allKeys]; NSEnumerator *keyEnum = [allKeys objectEnumerator]; NSMutableArray *destin = [NSMutableArray arrayWithCapacity:[allKeys count]]; id key; while(key = [keyEnum next]) [destin addObject:[[[ObjectKeyHolder alloc] initWithObject:[myDict objectForKey:key] key:key] autorelease]];
That should do the trick... if that's what you wanted.

HTH,
Matt Fahrenbacher
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 2, 2002, 01:40 PM
 
Let me show you first what I did and then I will go and try if what you suggested makes sense or not. This is what I had:

oldData = [[NSDictionary alloc] initWithDictionary:[oldPrefs objectForKey:@"TableView"]];

NSEnumerator *enumerator = [oldData keyEnumerator];
id key;

while ((key = [enumerator nextObject])) {
[records addObject:&key];
}

[tableView reloadData];


But it told me that there is an error BEFORE ID KEY?????????????????

I still don't know why or what or grrrr

OK let me go and try yours.
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 2, 2002, 02:03 PM
 
Sorry, but that didn't work either [not even when I cleaned your mistakes [NSEnumerator does not recognize "next"]

HEEEEEEEEEEEEEEEEEEELP!!!!!!!!!!!!
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Sep 2, 2002, 02:33 PM
 
Ohpps, I meant nextObject

Matt Fahrenbacher
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Sep 2, 2002, 02:35 PM
 
Actually, do all you want is an array of keys?

Then just use NSDictionary's allKeys method, it's very handy

Matt Fahrenbacher
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 2, 2002, 02:39 PM
 
Originally posted by Ghoser777:
Actually, do all you want is an array of keys?

Then just use NSDictionary's allKeys method, it's very handy

Matt Fahrenbacher
Keys WITH all the objects.
It gave me some errors when I used allKeys... let me try again.
     
VEGAN  (op)
Senior User
Join Date: Jun 2002
Location: UK
Status: Offline
Reply With Quote
Sep 2, 2002, 03:25 PM
 
If you need to know: IT WAS THE allValues!!!!! That did the trick... it was so simple in the end that I wanted to cry having messed most of the afternoon doing silly things
     
   
Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:44 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2