about NSMutabuleArray [closed]
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this questionruby code
irb(main):001:0> ary = ["a", "b", "b", "a", "b"]
irb(main):002:0> ary.uniq!
I want to write same code in objective-c.
You use a NSSet to ensure uniqueness. setWithArray
receives an array containing the objects to add to the new set. If the same object appears more than once in anArray, it is added only once to the returned set:
NSArray *arr = [[NSSet setWithArray: [NSArray arrayWithObjects: @"a", @"b", @"b", @"a", @"b", nil]] allObjects];
//If you want to obtain a mutable array:
NSMutableArray *mutArr = [NSMutableArray arrayWithArray: arr];
allObjects is used to return a NSArray representation of the NSSet, and this array contains all the unique objects in the initial array.
精彩评论