开发者

about NSMutabuleArray [closed]

Closed. This q开发者_如何学Pythonuestion does not meet Stack Overflow guidelines. It is not currently accepting answers.

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 question

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜