Unique elements in NSMutableSet !!!

Discussion in 'Mac Programming' started by saleh.hi.62, Jul 25, 2011.

  1. saleh.hi.62 macrumors member

    Joined:
    Jul 25, 2011
    #1
    Hello guys,

    i have got a problem with this object (NSMutableSet)!
    as what i have read over the objective-c's document, i read that this object contain unique elements ! so look at the code that i have written and tell me why does it add duplicate elements !!!

    Code:
    	NSMutableSet *mutableSet = [[NSMutableSet alloc]init];
    	[mutableSet addObject:@"value1"];
    	[mutableSet addObject:@"value1"];
    	[mutableSet addObject:@"value1"];
    and

    NSMutableSet *mutableSet =

    Code:
    [NSMutableSet setWithObjects:@"One", @"one", @"one", nil];
    i want to know why i can add duplicate elements since it must not be!
    i need to add unique elements, kindly help me how i can do this.

    thanks
     
  2. ChOas, Jul 25, 2011
    Last edited: Jul 25, 2011

    ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #2
    Code:
        NSMutableSet *mySet = [NSMutableSet setWithObjects:@"One", @"one", @"one", nil];
        NSLog(@"mySet: %@", mySet);
    
    prints:

    Code:
    2011-07-26 07:56:30.852 mytest[24466:902] mySet: {(
        One,
        one
    )}
    
    
    So the set only contains unique elements. Works perfect!

    The docs say:
    Code:
    The NSMutableSet class declares the programmatic interface to a mutable, unordered collection of distinct objects.
    
    It never says anywhere you can't add the same object to it multiple times. It only says it keeps a set of distinct objects.

    I think we have an XY problem again. What is it you want to accomplish ?
     
  3. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #3
    ok let me say in a different way, what i want to do is that to have a set of elements which are not duplicate!

    for example if i want to add "one" and it is added to the object already it return a error and say it is exist already there, this is what i want.
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    Please read the documentation for NSSet and NSMutableSet. The first thing you say is what NSSet does, the second thing is trivial if you read the documentation.
     
  5. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #5
    Then you just use containsObject: on your set to check if the object is already there and bail if needed.
     
  6. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #6
    thank you so much dude,

    I am new in Objective C! could you plz illuminate me with an example? i really dont know how to use it!
     
  7. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #7
    Code:
        NSMutableSet *mySet = [[NSMutableSet alloc] init];
    
        for (NSString *entry in [NSArray arrayWithObjects: @"1",@"2",@"3",@"1",@"5",@"2",@"2",nil]) {
    
         if ([mySet containsObject:entry]) {
          NSLog(@"Not adding %@, already in set",entry);
         } else {
          NSLog(@"Adding %@ to set",entry);
          [mySet addObject: entry];
         };
        };
    
    
     
  8. ChOas, Jul 26, 2011
    Last edited: Jul 26, 2011

    ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #8
    btw... I only started using sets yesterday, I'm a C programmer by origin and during my work now I usually program Perl (nobody dare laugh, okay ?)

    But try and work your way through the documentation, it really all makes sense.

    Like I said, I started sets yesterday. In my situation I need to add counted sets together. I had no clue if that was possible, so I extended NSCountedSet to allow me to add a set to a set I already had: [myCountedSet addCountedSet: newSet]. It's easy, just think about it and use the documentation.

    According to Perl TIMTOWTDI (There Is More Than One Way To Do It) I'm adapting. I might not have found the best way to do this (although I looked for a better way) but it works for me.

    Use the documentation, my friend!

    (just as a note: I am learning Obj-C in my spare time, for personal stuff, I am still fooling around)
     

Share This Page