Reading the apple docs on NSMutableSet, it is not clear in what order objects are stored in NSMutableSet or at least what order you will get one if you try to retrieve one. For example if send the anyObject message to an instance of NSMutableSet, do you get the first object or just any random object. The docs are not clear on that. So here is small piece of code that I ran to find out what is returned to us when anyObject is sent as a message.
// main.m // VariousTests // // Created by nata on 11/9/11. // Copyright (c) 2011 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { @autoreleasepool { NSString *name1, *name2, *name3; name1 = @"Name1"; name2 = @"Name2"; name3 = @"Name3"; //Create a set with string objects NSMutableSet *set1 = [NSMutableSet setWithObjects:name1,name2,name3,nil]; //Count them and print them NSInteger count = [set1 count]; NSLog(@"Count of the Set is : %ld", count); //Get any one of them id temp = [set1 anyObject]; //This will print the object contents //Notice in the output below it prints the first string CFShow(temp); //remove the second string from set [set1 removeObject:name2]; //clear temp temp = nil; //Get one more object and print //Notice it prints the same first string object temp = [set1 anyObject]; CFShow(temp); } return 0; }
Output:
[Switching to process 987 thread 0x0] 2011-11-18 20:34:50.717 VariousTests[987:903] Count of the Set is : 3 Name1 Name1 Program ended with exit code: 0
So it looks like any time you use the anyObject message on a set you will get the first object in the set. And I am hoping my conclusion is right. If anybody has a different analysis please do drop me a note!
0 comments:
Post a Comment