English 中文(简体)
从两个阵列中挑选独特数目
原标题:Getting unique numbers from two arrays

如果我有一对NSArrays填满intsNSNumbers,如:

A: { 12, 23, 45, 56, 67, 78, 99, 234 }
B: { 12, 56, 78, 99, 454, 512 }

我如何产生一个阵列,其编号为A,但不是B。

{ 23, 45, 67, 234 }
最佳回答

What you are tying to do is purely a set operation. So you can use NSSet here. You should do minusSet: to get the result you want.

NSMutableSet *resultSet = [NSMutableSet setWithArray:A];
NSSet *setB = [NSSet setWithArray:B];

// This is what you need!
[resultSet minusSet:setB];

Array *result = [resultSet allObjects];
问题回答

建立称为C的NSMutableArray。 Do a loop over A, which tries to seek each of its elements within B (using [B containedObject:elemOfA], which only sents the -isEqual:elemOfA information to each member of B. 如果发现某一要素,则无所作为;如果发现某一要素,则添加到C。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签