• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Issue with fast enumeration of dictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Issue with fast enumeration of dictionary


  • Subject: Issue with fast enumeration of dictionary
  • From: Bill Cheeseman <email@hidden>
  • Date: Thu, 24 Jul 2008 08:28:30 -0400
  • Thread-topic: Issue with fast enumeration of dictionary

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

  • Follow-Ups:
    • Re: Issue with fast enumeration of dictionary
      • From: Bryan Henry <email@hidden>
  • Prev by Date: Re: Icon button missing from XCode 3.1
  • Next by Date: Re: Issue with fast enumeration of dictionary
  • Previous by thread: Re: Icon button missing from XCode 3.1
  • Next by thread: Re: Issue with fast enumeration of dictionary
  • Index(es):
    • Date
    • Thread