-2

我试图向 NSMutableSet 添加一个对象,但它在与 NSMutableArray 完美配合的情况下不起作用。

//doesn't Work

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

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

如何在 NSMutableSet 中添加对象?

4

4 回答 4

3

arr_NSMutableSet是零。您需要创建它:

arr_NSMutableSet = [NSMutableSet set];
于 2014-11-21T06:26:02.430 回答
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"];

你可以试试这个

于 2014-11-21T06:30:42.013 回答
0

这通常发生在我身上,因为我忘记初始化集合(或数组,或字典......)。确保您正在执行以下操作:

NSMutableSet *mySet = [NSMutableSet set];

或者,或者:

NSMutableSet *mySet = [[NSMutableSet alloc] init];
于 2014-11-21T06:30:06.283 回答
0

像这样尝试,创建一个简单的字符串对象并添加到 Mutable 集合中,如下所示

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

它为我工作

于 2014-11-21T06:26:54.533 回答