Re: Issue with fast enumeration of dictionary
Re: Issue with fast enumeration of dictionary
- Subject: Re: Issue with fast enumeration of dictionary
- From: Bryan Henry <email@hidden>
- Date: Thu, 24 Jul 2008 07:36:28 -0500
Or rather, you have to declare NSDictionary *dictionary; and use that.
Coding in Mail.app gets ya every time.
Bryan
On Jul 24, 2008, at 7:32 AM, Bryan Henry wrote:
Fast enumeration in Objective-C enumerates through the keys, not
through the keys' objects. This is what you want:
+ (NSArray *)observersForPid:(pid_t)pid {
NSMutableArray *outArray = [NSMutableArray array];
for (id *key in observersDictionary) {
dictionary = [observersDictionary objectForKey:key];
if ([[dictionary objectForKey:@"pidNumberKey"] intValue] ==
pid) {
[outArray addObject:[dictionary
objectForKey:@"observerKey"]];
}
}
return [[outArray copy] autorelease]; // immutable
}
----
Bryan
On Jul 24, 2008, at 7:28 AM, Bill Cheeseman wrote:
In my NSDictionary object , the keys are NSValue objects encoding a
pointer
of type void*, using +[NSValue valueWithPointer:].
In the examples below, observersDictionary is the dictionary I am
enumerating. It is an NSDictionary object of global scope.
When I enumerate the dictionary the old Objective-C 1.0 way, it
works:
+ (NSArray *)observersForPid:(pid_t)pid {
NSMutableArray *outArray = [NSMutableArray array];
NSEnumerator *enumerator = [observersDictionary objectEnumerator];
NSDictionary *dictionary;
while ((dictionary = [enumerator nextObject])) {
if ([[dictionary objectForKey:@"pidNumberKey"] intValue] ==
pid) {
[outArray addObject:[dictionary
objectForKey:@"observerKey"]];
}
}
return [[outArray copy] autorelease];
}
But when I enumerate it using fast enumeration in Objecive-C 2.0,
it does
not work:
+ (NSArray *)observersForPid:(pid_t)pid {
NSMutableArray *outArray = [NSMutableArray array];
for (NSDictionary *dictionary in observersDictionary) {
if ([[dictionary objectForKey:@"pidNumberKey"] intValue] ==
pid) {
[outArray addObject:[dictionary
objectForKey:@"observerKey"]];
}
}
return [[outArray copy] autorelease]; // immutable
}
If I understand the error message correctly, it tells me that
dictionary
does not respond to the -objectForKey: selectors in the if clause.
Then, when I tweak the fast enumeration version to use an
enumerator, it
works again:
+ (NSArray *)observersForPid:(pid_t)pid {
NSMutableArray *outArray = [NSMutableArray array];
NSEnumerator *enumerator = [observersDictionary objectEnumerator];
for (NSDictionary *dictionary in enumerator) {
if ([[dictionary objectForKey:@"pidNumberKey"] intValue] ==
pid) {
[outArray addObject:[dictionary
objectForKey:@"observerKey"]];
}
}
return [[outArray copy] autorelease]; // immutable
}
I suspect this has to do with the fact that the dictionary's keys
encode
void* pointers. But the keys are NSValue objects, and they are
therefore
valid dictionary keys because they are of type id as specified by the
NSDictionary API. The Objective-C 2.0 language documentation states
that
dictionaries implement fast enumeration by enumerating on the keys.
What is going on? Should it be documented that you can't use fast
enumeration on a dictionary whose keys are NSValue objects created
using
+valueWithPointer:? Or is this a bug in Objective-C 2.0 or in the
implementation of fast enumeration for dictionaries? Or am I doing
something
wrong?
I'm using GCC 4.2. I haven't tried this with GCC 4.0.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
www.quecheesoftware.com
PreFab Software - www.prefabsoftware.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
@mac.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden