问题描述
在 mapView:didFinishLoadingStyle:
方法中,我希望能够检查 MGLSymbolStyleLayer
是否使用特定的 class
,类似于我们如何使用 filter
一个 JSON 样式文件,用于将样式仅应用于某个类:
但是,就我而言,我想检查并查看地图上是否存在一个类,如果它是字符串数组的一部分,则将其隐藏:
NSArray *poiTypesExcluded = @[@"airport"];
for (Nsstring *poiType in poiTypesExcluded) {
layer.visible = [NSExpression expressionWithFormat:@"class != %@",poiType];
}
这是给我的错误:
Unable to parse the format string "class != %@ == 1"
。
有关如何编写 NSExpression
以将 class
属性与另一个字符串进行比较的任何帮助?
解决方法
我在这上面浪费了很多时间,最后它比我想象的要容易:
NSArray *poiTypesExcluded = @[@"airport",@"grocery",@"bank"];
NSMutableArray *predicates = [[NSMutableArray alloc] init];
for (NSString *poiType in poiTypesExcluded) {
[predicates addObject:[NSPredicate predicateWithFormat:@"class != %@",poiType]];
}
symbolLayer.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];