React Native Bridging: Passing JSON to Swift function
Asked Answered
P

2

6

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.

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.

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: react native error message

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

Thanks

/Peter

Presence answered 29/11, 2016 at 21:52 Comment(3)
I haven't dealt with react too much, but I think it is because you have some syntax issues in your swift code. In your objective c code, you have a parameter: trackInfo:(NSDictionary *)trackInfo. In swift 3 this parameter would convert to ,trackInfo trackInfo: [String: Any] -- This may be your issue.Ava
If it is, you would probably want to change your methods. Something like: RCT_EXTERN_METHOD(startTrack:(NSString *)url withTrackinfo:(NSDictionary *)trackinfo withSlides:(NSDictionaryArray *)slides) and @objc func startTrack(_ url: String,withTrackInfo trackinfo: [String: Any],withSlides slides: [[String: Any]])Ava
Still the same error though. I have a feeling that it has to be something with the RCT_EXTERN_METHOD methods way to interpret the json object as a NSDictionary.Presence
A
8

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.

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

Objective-c

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

Swift

@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" }]
);

**notice that you have a typo on JS part

Andi answered 21/11, 2018 at 13:39 Comment(3)
you are a hero.Ham
It worked for swift 5.Splay
Is there any way to make slide conforms to url and typeVittorio
D
2

Your answer did help me alot but still i had one problem

it was fixed by adding _ in your swift code at start of the line

@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)
}              
Derzon answered 9/10, 2019 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.