我正在尝试使用 MGL_MATCH 表达式设置 Mapbox 地图图层中各个要素的填充颜色,但我不断得到:
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString mgl_color]:无法识别的选择器发送到实例...
func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) {
let layer = mapView.style!.layer(withIdentifier: "powiaty-1") as! MGLFillStyleLayer
let source = mapView.style!.source(withIdentifier: "composite") as! MGLVectorTileSource
let features = source.features(sourceLayerIdentifiers: NSSet(object: "powiaty") as! Set<String>, predicate: nil);
let query = NSMutableString(format: "MGL_MATCH(jpt_kod_je, ");
for feature in features {
let code = feature.attribute(forKey: "jpt_kod_je") as! String
let color = code.prefix(2) == "06" ? UIColor.red : UIColor.white;
query.appendFormat("%@, %@, ", code, color)
}
query.appendFormat("%@)", UIColor.white);
print(query);
layer.fillColor = NSExpression(format: "%@", query);
}
预期结果:
4 位代码(jpt_kod_je:字符串属性)以“06”开头的特征用红色填充,其他所有用白色填充。
实际结果:
Crash with the stack:
0 CoreFoundation 0x0000000106f456fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x00000001064e9ac5 objc_exception_throw + 48
2 CoreFoundation 0x0000000106f63ab4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000106f4a443 ___forwarding___ + 1443
4 CoreFoundation 0x0000000106f4c238 _CF_forwarding_prep_0 + 120
5 Mapbox 0x000000010575d629 _ZN24MGLStyleValueTransformerIN4mbgl5ColorEU8__strongP7UIColorS1_S4_E15toPropertyValueINS0_5style13PropertyValueIS1_EEEENSt3__19enable_ifIXntsr3std7is_sameIT_NS7_22ColorRampPropertyValueEEE5valueESC_E4typeEP12NSExpressionb + 257
6 Mapbox 0x000000010582845e -[MGLFillStyleLayer setFillColor:] + 123
7 MapboxExpressions 0x0000000105443eb3 $s17MapboxExpressions14ViewControllerC03mapC19DidFinishLoadingMapyySo06MGLMapC0CF + 4867 ...