ios - 如何从NativeScript调用Objective-C NSExpression(format:…)?

标签 ios objective-c nativescript

我正在扩展NativeScript-Mapbox plugin,以包括渲染随 map 缩放而改变大小的圆圈的功能。我正在研究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-platform-declarations .. objc!Foundation.d.ts NSExpression定义中列出的此调用的NativeScript类似物是:
static expressionWithFormatArgumentArray(expressionFormat: string, _arguments: NSArray<any>): NSExpression;

这是正确的方法吗?

如果是这样,如何将[12:2,22:180]转换为NSArray类型?

缺少文档,我尝试创建数组数组并将其传递给:
new NSArray( {objects: myArray} );

但它因段错误而崩溃。

显然,我缺少了一些东西。

最佳答案

您要处理的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}]);

关于ios - 如何从NativeScript调用Objective-C NSExpression(format:…)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54890753/

相关文章:

php - Apple 推送通知有时有效但并非总是有效

ios - 两个方向上的对象之间的固定空间

ios - 快速在 TableView 的最后一个元素下方添加空间

iphone - 用户启用安全登录时的 facebook 安全警告 - iPhone

javascript - 从 Nativescript 的 WebView 访问 header /查询参数

ios - init中返回nil会导致内存泄漏吗?

ios - 用于在 iOS 8 中显示 NSData 的 WKWebView loadData 替代方案(UIWebView 除外)

iphone - 关于调用我的属性

node.js - 构建时出现 Nativescript 错误

listview - NativeScript Angular 2 Firebase 列表不起作用