【发布时间】:2013-04-23 06:12:38
【问题描述】:
我想做的是创建一个NSMutableSet,其目的是计算有多少对唯一数据。
基本上,我有两个可变数组; xCoordinates 和 yCoordinates,以及一个名为 XYPoint 的自定义对象。重合索引处的每个 X 坐标和 Y 坐标结合起来在笛卡尔平面上形成一个点。例如,在索引 2 处,xCoordinates 数组中可能有数字 4,而在 yCoordinates 数组中可能有数字 8,从而得出点 (4, 8)。
现在,问题的关键是,我想做的是检查有多少 unique 点。我打算使用NSMutableSet 来做这件事。即:
for (int i = 0; i < [xCoordinates count]; i++) {
XYPoint *newXY = [[XYPoint alloc] init];
newXY.xCoordinate = [xCoordinates objectAtIndex:i];
newXY.yCoordinate = [yCoordinates objectAtIndex:i];
if ([distinct containsObject:newXY] == NO) {
[distinct addObject:newXY];
}
}
不幸的是,这不起作用。有没有办法说;
if (there isn't an object in the set with an identical X coordinate property and Y coordinate property){
Add one to the set;
}
?
【问题讨论】:
-
我猜 set 只允许唯一值,它不会在其中添加重复值。详情请refer this doc
-
检查这个问题/答案:stackoverflow.com/questions/10586218/…
标签: objective-c