Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
January 23, 2018, 09:08:34 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Exercise 15.10 Question  (Read 1307 times)
FogBurner
Newbie
*
Posts: 7


« on: May 17, 2010, 12:16:31 PM »

I tried using fast enumeration to cycle through the NSCountedSet but not understanding why the results come out in random order:
2010-05-17 16:10:33.250 chap15Ex10[740:a0f] Integer Frequency Chart
2010-05-17 16:10:33.254 chap15Ex10[740:a0f] -----------------------
2010-05-17 16:10:33.258 chap15Ex10[740:a0f] 5 | ****
2010-05-17 16:10:33.260 chap15Ex10[740:a0f] 1 | ***
2010-05-17 16:10:33.260 chap15Ex10[740:a0f] 6 | *
2010-05-17 16:10:33.261 chap15Ex10[740:a0f] 2 | ***
2010-05-17 16:10:33.263 chap15Ex10[740:a0f] 7 | *
2010-05-17 16:10:33.264 chap15Ex10[740:a0f] 3 | *****
2010-05-17 16:10:33.264 chap15Ex10[740:a0f] 8 | *

I sorted the array before loading it into the NSCounted set hoping it would influence the result but no dice.  I know I can take the output from the fast enum and load it into an array for sorted output but would like to understand the NSCountedSet resulting fast enum order.

Thanks
code below:

#import <Foundation/Foundation.h>
#define intObj(y) [NSNumber numberWithInt:y]
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSString *starString = [NSString stringWithString:@"**************************"];
    NSArray *anArray = [NSArray arrayWithObjects: intObj(3), intObj(5), intObj(Cool,
                  intObj(1), intObj(5), intObj(2),intObj(3), intObj(1),
                  intObj(2),intObj(3), intObj(5), intObj(2),intObj(3),
                  intObj(5), intObj(1),intObj(3), intObj(6), intObj(7),nil];
   
   NSArray *sortedArray = [anArray sortedArrayUsingSelector:@selector(compare:)];
   NSCountedSet *cSet = [[NSCountedSet alloc ]initWithArray: sortedArray];
   
   NSLog (@"Integer Frequency Chart");
   NSLog (@"-----------------------");

   for (NSNumber *aNumber in [cSet objectEnumerator])
      NSLog (@"%@ | %@",aNumber,[starString substringToIndex:[cSet countForObject:aNumber]]);
   
   [cSet release];
    [pool drain];
    return 0;
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: May 17, 2010, 12:38:33 PM »

As noted in the text, the elements in a set are unordered.   That also means that the order of adding elements to the set does not guarantee any ordering of their subsequent retrieval.

Cheers,

Steve Kochan
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.