MapboxMaps的ViewAnnotationManager在Xcode中编译错误
介绍
在ReactNative中使用了@rnmapbox/maps来集成Mapbox地图,开发应用时,在使用Xcode编译代码时,MapboxMaps包里面有一个报错:Cannot convert return expression of type '[UIView : ViewAnnotationOptions]' to return type 'Dictionary<String, Optional<JSONValue>>.RawValue' (aka 'Dictionary<String, Optional<Any>>'),记录一下处理方案。
版本信息
Dev OS:OSX 15.1.1Xcode:16.2Expo version:52.0.7ReactNative:0.76.3@rnmapbox/maps:10.1.33mapbox-gl:@rnmapbox/maps 10.1.33对应版本对应的版本^2.9.0。
注意 目前(2025-1-13)最新的mapbox-gl版本为v3.9.2。
问题
在Xcode中编译代码时,MapboxMaps包里面有一个报错。报错位置为:Pods->Pods->MapboxMaps->ViewAnnotationManager。
报错的76-82行如下:
/// The complete list of annotations associated with the receiver.
@available(*, deprecated, message: "Use ViewAnnotation")
public var annotations: [UIView: ViewAnnotationOptions] {
idsByView.compactMapValues { [mapboxMap] id in
try? mapboxMap.options(forViewAnnotationWithId: id)
}
}在idsByView.compactMapValues { [mapboxMap] id in这行代码这里提示:
not convert return expression of type '[UIView : ViewAnnotationOptions]' to return type 'Dictionary<String, Optional<JSONValue>>.RawValue' (aka 'Dictionary<String, Optional<Any>>')分析
通过annotations属性的源代码,可以看出,该代码已经被标记为@available(*, deprecated, message: "Use ViewAnnotation"),表示这个属性已经过时,建议使用ViewAnnotation。我们本身应该没有使用这个属性,但是MapboxMaps包里面有一个ViewAnnotationManager类,这个类里面使用了这个属性,导致编译错误。
看报错内容,可以知道这是一个类型错误,[UIView : ViewAnnotationOptions]不能转换为Dictionary<String, Optional<JSONValue>>.RawValue,即Dictionary<String, Optional<Any>>。
既然只是一个类型转换错误,我们又没有使用,那么我们可以忽略掉这个转换。
解决方案
这里我们之间返回一个空字典即可,修改ViewAnnotationManager的annotations属性,如下:
/// The complete list of annotations associated with the receiver.
@available(*, deprecated, message: "Use ViewAnnotation")
public var annotations: [UIView: ViewAnnotationOptions] {
return [:]
}修改完成之后,再次编译,则通过了编译了。运行我们的应用,没有问题。
总结
上面的问题我已经在rnmapbox/maps中提交了一个issue。not convert return expression of type '[UIView : ViewAnnotationOptions]' to return type ... #3738
这个问题是由于MapboxMaps包里面有一个过时的属性,导致编译错误。我们通过修改这个属性,使其返回一个空字典,从而解决了编译错误。我们是通过@rnmapbox/maps来使用Mapbox的,@rnmapbox/maps 10.1.33对应版本对应的版本^2.9.0。目前最新的mapbox-gl-js版本为v3.9.2。所以很有可能是@rnmapbox/maps使用了太旧的mapbox-gl版本导致的,需要更新并兼容新版本才能从根本上解决问题。
注意 上面的原因,只是猜测,如果要确认,则需要去mapbox-gl的源码中去看看。
参考
not convert return expression of type '[UIView : ViewAnnotationOptions]' to return ... #3738
