React Native应用程序纯粹在Swift中
我使用以下命令创建了我的第一个本机应用程序。
react-native init PropertyFinder
这为Objective-C中的 ios创建了一个项目。 问题是我不知道Objective-C但我在Swift上工作。
这个要点展示了如何将反应组件链接到快速项目。 但有没有办法直接用Swift语言创建项目?
这篇优秀的post帮我解决了这个问题。
解
- 添加桥接标题
#import "RCTBridgeModule.h" #import "RCTBridge.h" #import "RCTEventDispatcher.h" #import "RCTRootView.h" #import "RCTUtils.h" #import "RCTConvert.h"
- 添加AppDelegate.Swift
var bridge: RCTBridge! func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. /** * Loading JavaScript code - uncomment the one you want. * * OPTION 1 * Load from development server. Start the server from the repository root: * * $ npm start * * To run on device, change `localhost` to the IP address of your computer * (you can get this by typing `ifconfig` into the terminal and selecting the * `inet` value under `en0:`) and make sure your computer and iOS device are * on the same Wi-Fi network. */ let jsCodeLocation = NSURL(string: "http://localhost:8081/index.ios.bundle?platform=ios&dev=true") /** * OPTION 2 * Load from pre-bundled file on disk. The static bundle is automatically * generated by "Bundle React Native code and images" build step. */ // jsCodeLocation = NSBundle.mainBundle().URLForResource("main", withExtension: "jsbundle") let rootView = RCTRootView(bundleURL:jsCodeLocation as URL!, moduleName: "PropertyFinder", initialProperties: nil, launchOptions:launchOptions) self.bridge = rootView?.bridge self.window = UIWindow(frame: UIScreen.main.bounds) let rootViewController = UIViewController() rootViewController.view = rootView self.window!.rootViewController = rootViewController; self.window!.makeKeyAndVisible() return true }
-
删除
AppDelegate.h
,AppDelegate.m
和主文件。 -
清洁和建设。
如果您在import时遇到问题
#import "RCTBridgeModule.h" #import "RCTBridge.h" #import "RCTEventDispatcher.h" #import "RCTRootView.h" #import "RCTUtils.h" #import "RCTConvert.h"
尝试
#import #import #import #import #import #import
这实际上对我有用:
import Foundation import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var bridge: RCTBridge! func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let jsCodeLocation: URL jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index.ios", fallbackResource:nil) let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "REPLACE_WITH_YOUR_PROJECT_NAME", initialProperties: nil, launchOptions: launchOptions) let rootViewController = UIViewController() rootViewController.view = rootView self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.rootViewController = rootViewController self.window?.makeKeyAndVisible() return true } }
我调查了它, react-native
不支持该function。
react-native init
正在做的react-native init
是获取模板并更改其中的一些属性。
模板是用Objective-C编写的,因此除了手动修改结果之外,您无法做任何事情。
但是,这不应该真正关心你。 即使在Objective-C项目中也可以使用Swift。 无需将文件重写为Swift。