【发布时间】:2017-02-01 22:39:01
【问题描述】:
我正在创建一个包含 Swift CocoaPod 库的 NativeScript 插件。
在我的NativeScript plugin 中,我使用一个单独的类将所需的delegate 添加到主类。
是否可以在主类中实现委托方法,完全避免委托类?
即,
export class MicrosoftBandService extends NSObject implements ConnectionDelegate {
static new(): MicrosoftBandService {
return <MicrosoftBandService>super.new()
}
constructor() {
let _self= this;
this.mbk = MicrosoftBand.alloc().init();
this.mbk.connectDelegate = _self
}
onConnecte() {
//
onDisconnecte() {
//
}
onErrorWithError(error) {
//
}
}
更好,我喜欢这样做
export class MicrosoftBandService extends MicrosoftBand implements ConnectionDelegate {
static new(): MicrosoftBandService {
return <MicrosoftBandService>super.new()
}
constructor() {
let _self= this;
this.connectDelegate = _self
}
onConnecte() {
//
}
onDisconnecte() {
//
}
onErrorWithError(error) {
//
}
}
我不知道在 TypeScript for NativeScript 中使用自委托实现构造函数的正确语法
我当前的代码有效,但我正在寻求帮助,以通过消除单独的委托类来简化和减少代码。
【问题讨论】:
-
我只想添加一条评论;如果你可以避免使用 Swift;你最好这样做。 Swift 为应用程序增加了超过 20 兆的额外运行时。
标签: swift angular typescript nativescript angular2-nativescript