NSSet containsObject不起作用? [英] NSSet containsObject Not Working?
问题描述
我试图在将注释添加到数组之前检查注释是否存在. 我不知道为什么[visibleAnnotations containsObject:annotation]总是返回False.
I am trying to check if the annotation exists before adding it to array. I can't figure out why [visibleAnnotations containsObject:annotation] always returns False.
MKMapRect mRect = self.locationView.visibleMapRect;
NSSet *visibleAnnotations =[self.locationView annotationsInMapRect:mRect];
NSLog(@"Annotation in Rect %@",visibleAnnotations);
for(NSArray * obj in JSON){
coordinates.latitude=[[obj valueForKey:@"Latitude"] doubleValue];
coordinates.longitude=[[obj valueForKey:@"Longitude"] doubleValue];
NSString *title=[NSString stringWithFormat:@"%@",[obj valueForKey:@"DeviceId"]];
MapAnnotation *annotation = [[MapAnnotation alloc] initWithTitle:title andCoordinate:coordinates] ;
NSLog(@"New Annotation %@",annotation);
if ([visibleAnnotations containsObject:annotation ]) {
//[addPins addObject:annotation];
NSLog(@"Contains obj");
}
[addPins addObject:annotation];
[annotation release];
}
缩放以仅显示1个注释.
Zoomed to only show 1 annotation.
Annotation in Rect {(<MapAnnotation: 0x1cd80720>)}
New Annotation <MapAnnotation: 0x1cd79410>
谢谢
推荐答案
如果一旦创建并添加到集合后再也没有更改MapAnnotation
实例的属性,则可以覆盖hash
和isEqual:
该类的方法来获取您在此处寻找的行为.
If you never change the properties of your MapAnnotation
instances once they are created and added to the set, you could override the hash
and isEqual:
methods for that class to get the behavior you're looking for here.
NSSet
使用这两种方法来测试对象;如果重新定义isEqual:
以比较MapAnnontation
对象中包含的值,而不是对象本身的标识,则集合将使它们相等.
NSSet
uses those two methods to test objects; if you redefine isEqual:
to compare the values contained in the MapAnnontation
objects, rather than the identity of the objects themselves, the set will take them to be equal.
这篇关于NSSet containsObject不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!