React Native Bridged:将JSON传递给Swift函数 [英] React Native Bridging: Passing JSON to Swift function

查看:156
本文介绍了React Native Bridged:将JSON传递给Swift函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift和Objective-C的新秀,但我试图建立从React Native到Swift的桥梁,并将JSON和JSON数组作为参数发送给Swift.

Im a rookie in Swift and Objective-C but Im trying to make a bridge from React Native to Swift, and send a JSON and a JSON Array as params to Swift.

在React内部,我想调用此函数:

Inside React I would like to call this function:

startTrack('some url string', { artist: 'Bruce Springsteen', title: 'Born in the USA' }, [{ url: 'url', type: 'image' }, { url: 'another url', type: 'link' ]})

一个字符串,一个对象和一个对象数组.

So a string, an object, and a object array.

在我的bridge目标-c文件中,我有以下内容:

In my bridge objective-c file i have this:

RCT_EXTERN_METHOD(startTrack:(NSString *)url trackinfo:(NSDictionary *)trackinfo slides:(NSDictionaryArray *)slides)

在我的快速文件中,我尝试了很多组合,但是似乎没有任何效果:

And in my swift file i have tried a lot of combinations but nothing seems to go through:

  @objc func startTrack(url: String, trackinfo: [String: Any], slides: [[String: Any]]) {
    print("Play test", url, trackinfo, slides)
  }

我收到此错误消息:

I get this error message:

如何将参数一直发送到Swift文件?

How can I send my params all the way through to my Swift file?

谢谢

/彼得

推荐答案

您的Objective-c方法签名将trackingInfo设置为NSDictionary,但是您的Swift方法签名接受了Dictionary<String: Any>,因此这两个签名不匹配

Your objective-c method signature set trackingInfo as NSDictionary, but your Swift method signature accepts a Dictionary<String: Any>so this two signature doesn't match.

您必须重新编写方法并手动管理NSDictionary到Dictionary的演员表:

You have to re-write your method and manually manage NSDictionary to Dictionary cast:

RCT_EXTERN_METHOD(startTrack:(NSString *)url trackinfo:(NSDictionary *)trackinfo slides:(NSDictionaryArray *)slides)

迅速

@objc func startTrack(url: String, trackinfo: NSDictionary, slides: [NSDictionary]) {
    guard let infoDictionary = trackinfo as? [String: Any],
          let slidesDictionary = slides as? [[String: Any]] else {
        return
    } 
    print("Play test", url, infoDictionary, slidesDictionary)
}

JS

startTrack(
  "some url string",
  { artist: "Bruce Springsteen", title: "Born in the USA" },
  [{ url: "url", type: "image" }, { url: "another url", type: "link" }]
);

**请注意,您在JS部分上有错字

**notice that you have a typo on JS part

这篇关于React Native Bridged:将JSON传递给Swift函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆