 |
 |
Enumerate
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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 
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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
|
|
Ohpps, I meant nextObject
Matt Fahrenbacher
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Actually, do all you want is an array of keys?
Then just use NSDictionary's allKeys method, it's very handy
Matt Fahrenbacher
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jun 2002
Location: UK
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

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