Ios addObject不使用NSMutableSet

Ios addObject不使用NSMutableSet,ios,objective-c,nsmutablearray,nsmutableset,Ios,Objective C,Nsmutablearray,Nsmutableset,我曾尝试向NSMutableSet添加一个对象,但在与NSMutableArray完美配合的情况下,它无法工作 //doesn't Work [arr_NSMutableSet addObject:Object]; NSLog(@"%@",arr_NSMutableSet); // Shows nil //Works fine [arr_NSMutableArray addObject:Object]; NSLog(@"%@",arr_NSMutableArray); // Shows re

我曾尝试向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中添加对象?

arr\u NSMutableSet
为零。您需要创建它:

arr_NSMutableSet = [NSMutableSet set];

尝试这样做,创建一个简单的字符串对象并添加到可变集合,如folows

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

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

NSMutableSet *mySet = [NSMutableSet set];
或者,或者:

NSMutableSet *mySet = [[NSMutableSet alloc] init];

检查初始化后,您可以尝试此操作

如何创建
arr\u NSMutableSet
。可能你没有初始化它。是的,没错。我没有初始化它。我真蠢:谢谢你的评论@ismailThanx。它工作得非常好。那是我愚蠢的错误:D。我的声誉低于15,这就是为什么我不能投票支持你的答案:(是的,没错。我没有初始化它。我太傻了:谢谢你的评论:)是的,没错。我没有初始化它。我真蠢:谢谢你的评论:)是的,没错。我没有初始化它。我真蠢:谢谢你的评论:)
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"];