NSMutableSet的使用

2017-06-05  本文已影响0人  菠罗吹雪

NSMutableSet的使用

- (void)updateMapViewAnnotationsWithAnnotations:(NSArray *)annotations

{

/* 用户滑动时,保留仍然可用的标注,去除屏幕外标注,添加新增区域的标注 */

NSMutableSet *before = [NSMutableSet setWithArray:self.mapView.annotations];

[before removeObject:[self.mapView userLocation]];

NSSet *after = [NSSet setWithArray:annotations];

//之前是123

//之后是2345

//123,2345

/* 保留仍然位于屏幕内的annotation. */

NSMutableSet *toKeep = [NSMutableSet setWithSet:before];

[toKeep intersectSet:after];//留下相同的。//23留下

/* 需要添加的annotation. */

NSMutableSet *toAdd = [NSMutableSet setWithSet:after];

[toAdd minusSet:toKeep];////向集合中删除另一个集合出现的元素//删除相同的,除 了23的,45要添加

/* 删除位于屏幕外的annotation. */

NSMutableSet *toRemove = [NSMutableSet setWithSet:before];

[toRemove minusSet:after];//向集合中删除另一个集合出现的元素// 23相同,只有1不同,删除

/* 更新. */

dispatch_async(dispatch_get_main_queue(), ^{

[self.mapView addAnnotations:[toAdd allObjects]];

[self.mapView removeAnnotations:[toRemove allObjects]];

});

}

上一篇 下一篇

猜你喜欢

热点阅读