-2

I've tried to add an Object to the NSMutableSet but it doesn't work where it perfectly works fine with NSMutableArray.

//doesn't Work

[arr_NSMutableSet addObject:Object];
NSLog(@"%@",arr_NSMutableSet); // Shows nil

//Works fine
[arr_NSMutableArray addObject:Object];
NSLog(@"%@",arr_NSMutableArray); // Shows result

How to add the Object in NSMutableSet?

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
Rahul
  • 211
  • 2
  • 18

4 Answers4

3

arr_NSMutableSet is nil. You need to create it:

arr_NSMutableSet = [NSMutableSet set];
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanx a lot. It worked perfectly fine. That was my silly mistake :D . And my reputation is below 15 that's why not able to up vote your answer :( – Rahul Nov 21 '14 at 06:29
2
NSMutableSet *brokenCars = [NSMutableSet setWithObjects:
                            @"Honda Civic", @"Nissan Versa", nil];
NSMutableSet *repairedCars = [NSMutableSet setWithCapacity:5];
// "Fix" the Honda Civic
[brokenCars removeObject:@"Honda Civic"];
[repairedCars addObject:@"Honda Civic"];

You can try this

Nikita Khandelwal
  • 1,741
  • 16
  • 25
  • Yeah, that's right. I didn't initialized it. So stupid of me :D. Thanx for your comment :) – Rahul Nov 21 '14 at 07:03
0

Try like this, created a simple string object and added to the Mutable set like as folows

NSMutableSet *customSet = [NSMutableSet set];
NSString *str = @"str";
[customSet addObject:str];

Its working for me

Alex Andrews
  • 1,498
  • 2
  • 19
  • 33
  • Yeah, that's right. I didn't initialized it. So stupid of me :D. Thanx for your comment :) – Rahul Nov 21 '14 at 07:02
0

This usually happens to me because I forgot to initialize the set (or array, or dictionary...). Make sure you are doing the following:

NSMutableSet *mySet = [NSMutableSet set];

or, alternatively:

NSMutableSet *mySet = [[NSMutableSet alloc] init];
Kai Schaller
  • 443
  • 3
  • 9
  • Yeah, that's right. I didn't initialized it. So stupid of me :D. Thanx for your comment :) – Rahul Nov 21 '14 at 07:02