【问题标题】:Marshalling setup when using obj-c class in nativescript (with typescript) project在 nativescript (with typescript) 项目中使用 obj-c 类时的编组设置
【发布时间】:2020-12-26 17:23:54
【问题描述】:

按照https://nativescript.org/blog/adding-objective-c-code-to-a-nativescript-app/ 上的说明,我设置了一个非常简单的 nativescript 项目(下面有 typescript):

  1. tns 创建 simpleApp --tsc
  2. tns平台添加ios
  3. tns 准备 ios
  4. 通过 xcode 运行生成的 ios 项目。

应用程序运行正常。

接下来我在项目中添加了一个自定义的 obj-c 类(App_Resources/iOS/src/Stuff .h 和 .m):

#import <Foundation/Foundation.h>

@interface Stuff: NSObject
+(void)doStuff;
@end
#import "Stuff.h"

@implementation Stuff
+(void)doStuff {
    NSLog(@"I should see this...");
}
@end

还有一个module.modulemap:

module Stuff {
    header "Stuff.h"
    export *
}

tns prapare ios 命令成功处理了该类。

当我尝试将“Stuff”对象添加到打字稿源时,我得到一个cannot find name Stuff。 如何让 typescript 和 obj-c 之间的编组/元数据工作?我需要运行一些脚本吗?添加进口?教程没有提到任何关于那...

这是我添加到 nativescript 默认模板应用程序中的:

onTap() {
        this._counter--;
        this.updateMessage();
        Stuff.doStuff();
    }

并希望在点击按钮时看到日志文本...

【问题讨论】:

    标签: objective-c typescript nativescript metadata marshalling


    【解决方案1】:

    我找到了一种将所需的 .d.ts 文件添加到项目中的方法: generating-typescript-typings

    在您的 nativescript 项目的 src-root 中运行以下命令:

    TNS_TYPESCRIPT_DECLARATIONS_PATH="$(pwd)/typings" tns build ios
    

    它会生成一个类型文件夹,其中包含 ios 类的所有 .d.ts 文件,包括手动创建的 Stuff 类。现在可以正确解释 Typescript 代码......新的 obj-c 类一旦被上面的行处理,就可以在 TS 代码中寻址。

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2019-03-21
      • 2011-12-15
      • 2016-06-06
      • 1970-01-01
      相关资源
      最近更新 更多