我正在对NativeScript-Mapbox plugin进行扩展,以包括渲染随着地图缩放而调整大小的圆的功能。我在example on this page上工作。
在iOS上,我在尝试将此NSExpression调用转换为NativeScript时遇到了问题:
layer.circleRadius = [NSExpression expressionWithFormat: @"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'exponential', 1.75, %@)",
@{@12: @2, @22: @180}];
在我看来,tns平台声明中列出的与此调用类似的NativeScript。objc!Foundation.d.ts NSExpression定义为:
static expressionWithFormatArgumentArray(expressionFormat: string, _arguments: NSArray<any>): NSExpression;
这是正确的方法吗?
如果是这样,如何将12: 2,22: 180转换为NSArray类型?
由于缺乏文档,我尝试创建一个数组数组并将其传递给:
new NSArray( {objects: myArray} );
但它崩溃得很可怕,并出现了一个段错误。
显然,我遗漏了一些东西。
发布于 2019-03-03 05:06:57
好了,只是循环回来完成这个线程,我已经/终于/想出了如何让它在不崩溃的情况下工作。
该方法需要一个包含NSDictionary类型的数组,并且对于this issue和this one,NSDictionary的NativeScript类型定义似乎是不正确的。
因此,经过几天的反复试验,将Objective-C调用转换为:
layer.circleRadius = [NSExpression expressionWithFormat: @"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'exponential', 1.75, %@)",
@{@12: @2, @22: @180}];
转换为TypeScript/NativeScript是完全违反直觉的:
let nsDict = new (NSDictionary as any)( [ 2, 180 ], [ 12, 22 ] );
let nsArray = NSArray.arrayWithArray( [ nsDict ] );
layer.circleRadius = NSExpression.expressionWithFormatArgumentArray(
"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'exponential', 1.75, %@)", nsArray );
这将编译并运行。请注意,键位于值之后的单独数组中,而不是反过来。
如果您使用上面建议的NSDictionary方法,您会得到一个编译错误,因为很明显类型不支持这种表示法。
升级到最新的NativeScript和核心模块确实允许它编译建议的NSArray() []引用,但结果代码会导致异常:即:
const expr = NSExpression.expressionWithFormatArgumentArray( "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'exponential', 1.75, %@)", [{12: 2, 22: 180}]);
崩溃并引发异常。
据我所知,该数组没有正确地封送到NSDictionary实例中。与使用NSArray.arrayWithArray( ... )相同;
发布于 2019-02-28 04:26:56
您正在处理的swift (不是objective-c)对象不是一个数组,而是一个字典。
import Swift
print(type(of: [12: 2, 22: 180]))
// Dictionary<Int, Int>
它所期望的数组是您想要的所有格式替换参数的数组;在本例中,该数组的长度为1
,并且只包含字典。您可以将其转换为您的NativeScript JavaScript/TypeScript:
const expr = NSExpression.expressionWithFormatArgumentArray(
"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'exponential', 1.75, %@)",
[{12: 2, 22: 180}]);
https://stackoverflow.com/questions/54890753
复制相似问题