提交 56e23d03 编写于 作者: fxy060608's avatar fxy060608

wip(uts): update compiler

上级 32c1657e
import "Foundation";
//
// TestModuleImpl.swift
// libWeex
//
// Created by dcloud on 2022/6/1.
// Copyright © 2022 DCloud. All rights reserved.
//
@objcMembers
class TestModuleImpl extends NSObject {
testAsyncFunc(option: NSDictionary, callback: UniModuleKeepAliveCallback) {
// options 为 js 端调用此方法时传递的参数
console.log(option)
// // 可以在该方法中实现原生能力,然后通过 callback 回调到 js
//
// // 回调方法,传递参数给 js 端 注:只支持返回 String 或 NSDictionary (map) 类型
// 第一个参数为回传给js端的数据,第二个参数为标识,表示该回调方法是否支持多次调用,如果原生端需要多次回调js端则第二个参数传 YES;
callback('success', false)
}
/// 同步方法(注:同步方法会在 js 线程执行)
/// @param options js 端调用方法时传递的参数
testSyncFunc(option: NSDictionary): string {
console.log(option)
return ''
}
}
//
// TestSwiftComponent.swift
// DCTestSwiftPlugin
//
// Created by Dcloud-XHY on 2022/6/28.
//
import 'UIKit'
import 'MapKit'
// 必须添加 @objc(类名),编译器才会生成对应的 oc 方法
@objc(TestSwiftComponent)
class TestSwiftComponent implements DCUniComponent, MKMapViewDelegate {
mapLoadedEvent = false
showTraffic = false
mapView?: MKMapView = null
// 初始化方法
override onCreateComponent(
ref: String,
type: String,
styles: [AnyHashable: Any],
attributes: [AnyHashable: Any],
events: [Any],
uniInstance: DCUniSDKInstance
) {
// NSDictionary 转换为 swift 的 [AnyHashable : Any] ,解析参数需要使用 [AnyHashable("key")]
this.showTraffic = DCUniConvert.bool(
attributes[AnyHashable('showTraffic')] ?? false
)
}
override loadView(): UIView {
this.mapView = MKMapView.init()
return this.mapView!
}
override viewDidLoad() {
this.mapView!.delegate = this
if (this.showTraffic) {
this.mapView.showsTraffic = true
}
}
// 监听属性变化方法
override updateAttributes(attributes: [AnyHashable: Any] =new Dictionary<AnyHashable, Any>()) {
if (attributes['showsTraffic'] != null) {
this.showTraffic = DCUniConvert.bool(
attributes[AnyHashable('showsTraffic')]!
)
this.mapView.showsTraffic = this.showTraffic
}
}
// 监听注册事件方法
override addEvent(eventName: String) {
if (eventName == 'mapLoaded') {
this.mapLoadedEvent = true
}
}
override removeEvent(eventName: String) {
if (eventName == 'mapLoaded') {
this.mapLoadedEvent = false
}
}
@objc public static wx_export_method_0(): String {
return 'focus:'
}
@objc focus(options: NSDictionary) {
print(options)
}
// MARK: - MKMapViewDelegate
mapViewDidFinishLoadingMap(mapView: MKMapView) {
if (this.mapLoadedEvent) {
// 回调 event 事件
this.fireEvent('mapLoaded', { mapLoaded: 'success' })
}
}
}
//
// TestSwiftModule.swift
// DCTestSwiftPlugin
//
// Created by Dcloud-XHY on 2022/6/27.
//
//import UIKit
// 必须添加 @objc(类名),编译器才会生成对应的 oc 方法
@objc(TestSwiftModule)
class TestSwiftModule extends DCUniModule {
// 暴露异步方法供js调用:
// 1.必须添加 @objc 前缀,
// 2.使用 public static 静态方法
// 3.方法名称必须是以 wx_export_method_ 开头,后面随意拼接一个字符保证唯一性即可
// 4.方法返回实例方法的名称
// 注意点:swift 方法名称转换为 oc 方法名称时系统会自动将首个参数用 with 拼接到方法名中作为方法名的一部分 如下实例
// testAsyncFunc(options:callback:) -> testAsyncFuncWithOptions:callback
// js 中调用方法 testAsyncFuncWithOptions(options,callabck)
// 可以在参数前面加上 _ 让编译器忽略拼接参数
@objc public static wx_export_method_0() : String {
return "testAsyncFunc::"
}
// 需要暴露的实例方法
// 1.必须添加 @objc 前缀
// 2.首个参数需要添加 _ 忽略外部参数 其他参数可选
@objc testAsyncFunc(options: NSDictionary,callback?: UniModuleKeepAliveCallback) {
print(options)
if (callback != null) {
callback!("success",false)
}
}
// 暴露同步方法供js调用:
// 1.必须添加 @objc 前缀,
// 2.使用 public static 静态方法
// 3.方法名称必须是以 wx_export_method_sync_ 开头,后面随意拼接一个字符保证唯一性即可
// 4.方法返回实例方法的名称
// 注意点:swift 方法名称转换为 oc 方法名称时系统会自动将首个参数名首字符大写然后用 with 拼接到方法名中作为方法名的一部分 如下实例
// testSyncFunc(options:) -> testSyncFuncWithOptions:
// js 中调用方法 var value = testSyncFuncWithOptions(options)
@objc public static wx_export_method_sync_0() : String {
return "testSyncFunc:"
}
// 需要暴露的实例方法
// 1.必须添加 @objc 前缀
// 2.首个参数需要添加 _ 忽略外部参数
@objc testSyncFunc(options: NSDictionary) : String {
print(options);
// 暴露方法内部可以调用其他 swift 方法
return getSomeString();
}
// 不需要直接暴露给js的方法不需要添加前缀
getSomeString() : String {
return "getSomeString success"
}
}
import Foundation;
@objcMembers
class TestModuleImpl : NSObject {
func testAsyncFunc(_ option: NSDictionary, _ callback: UniModuleKeepAliveCallback) {
console.log(option);
callback("success", false);
}
func testSyncFunc(_ option: NSDictionary) -> String {
console.log(option);
return "";
}
}
import UIKit;
import MapKit;
@objc(TestSwiftComponent)
class TestSwiftComponent : DCUniComponent, MKMapViewDelegate {
var mapLoadedEvent = false;
var showTraffic = false;
var mapView: MKMapView? = nil;
override func onCreateComponent(_ ref: String, _ type: String, _ styles: [AnyHashable: Any], _ attributes: [AnyHashable: Any], _ events: [Any], _ uniInstance: DCUniSDKInstance) {
self.showTraffic = DCUniConvert.bool(attributes[AnyHashable("showTraffic")] ?? false);
}
override func loadView() -> UIView {
self.mapView = MKMapView.init();
return self.mapView!;
}
override func viewDidLoad() {
self.mapView!.delegate = self;
if (self.showTraffic) {
self.mapView.showsTraffic = true;
}
}
override func updateAttributes(_ attributes: [AnyHashable: Any] = Dictionary<AnyHashable, Any>()) {
if (attributes["showsTraffic"] != nil) {
self.showTraffic = DCUniConvert.bool(attributes[AnyHashable("showsTraffic")]!);
self.mapView.showsTraffic = self.showTraffic;
}
}
override func addEvent(_ eventName: String) {
if (eventName == "mapLoaded") {
self.mapLoadedEvent = true;
}
}
override func removeEvent(_ eventName: String) {
if (eventName == "mapLoaded") {
self.mapLoadedEvent = false;
}
}
@objc
public static func wx_export_method_0() -> String {
return "focus:";
}
@objc
func focus(_ options: NSDictionary) {
print(options);
}
func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
if (self.mapLoadedEvent) {
self.fireEvent("mapLoaded", {
mapLoaded: "success"
});
}
}
}
@objc(TestSwiftModule)
class TestSwiftModule : DCUniModule {
@objc
public static func wx_export_method_0() -> String {
return "testAsyncFunc::";
}
@objc
func testAsyncFunc(_ options: NSDictionary, _ callback: UniModuleKeepAliveCallback?) {
print(options);
if (callback != nil) {
callback!("success", false);
}
}
@objc
public static func wx_export_method_sync_0() -> String {
return "testSyncFunc:";
}
@objc
func testSyncFunc(_ options: NSDictionary) -> String {
print(options);
return getSomeString();
}
func getSomeString() -> String {
return "getSomeString success";
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册