Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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
 
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 ?
 
Last edited:
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 ?

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.
 
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.

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.
 
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.

Then you just use containsObject: on your set to check if the object is already there and bail if needed.
 
Then you just use containsObject: on your set to check if the object is already there and bail if needed.

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!
 
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!

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];
     };
    };
 
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!

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)
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.