Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

React Native Native Module - Passing JSON prop to Swift UIView

$
0
0

I'm creating a Native Module in my RN project which includes a view with a JSON prop like so:

<MyNativeView name={{ firstName: "John", lastName: "Doe" }} />

The view is built in Swift and I have a setter for the name prop. Here's my current setup

// MyNativeViewManager.m#import "React/RCTViewManager.h"@interface RCT_EXTERN_MODULE(MyNativeViewManager, RCTViewManager)RCT_EXPORT_VIEW_PROPERTY(name, NSDictionary *)@end
// MyNativeViewManager.swift@objc(MyNativeViewManager)class MyNativeViewManager: RCTViewManager {  override func view() -> UIView! {    return MyNativeView()  }  // ...}// MyNativeView.swiftclass MyNativeView: UIView {  @objc(setName:)  func setName(name: NSDictionary) {    var firstName: String?    var lastName: String?    // This feels ugly... but does the job    if let firstNameProp = name["firstName"] as? String {      firstName = firstNameProp    }    if let lastNameProp = name["lastName"] as? String {      lastName = lastNameProp    }    // Do stuff...  }}

I was wondering if there was a way to convert this property into a struct so I can nest additional JSON or throw an error if particular fields are missing.

If I'm understanding the docs correctly, it looks like I could use RCT_CUSTOM_VIEW_PROPERTY and RCTConvert to convert a property into a defined type, but I'm not clear on how to do this in conjunction with Swift.


Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>