简体   繁体   中英

How to convert this method from Objective-C to Swift?

I'm trying to convert a method from the React-native Firebase documentation, from Objective-c code to Swift. But it's not working, can someone lend me a help?

The doc I'm following: https://rnfirebase.io/docs/v4.3.x/notifications/ios

This is the Object C part I want to convert and add to the Swift file:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}

At the moment, this is my code, see the commented part:

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  var bridge: RCTBridge!

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    RNFirebaseNotifications.configure()

    let jsCodeLocation: URL

    jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource:nil)
    let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "Zepplin", 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
  }

  /*func application(_ application: UIApplication, didReceiveLocalNotification notification: [UILocalNotification: Any]?) -> Any {
    RNFirebaseNotifications.instance(_, didReceiveLocalNotification, notification)
  }*/

}

But of corse, it's not working, because the syntax is wrong... I'm not familiar with Swift. Can someone lend me a hand? I would be glad! Thankss in before-hand!!!

EDIT:

Sorry if my question was not clear enough. For me, the question was, "how to fix the syntax?", I knew that the syntax was wrong. I had nothing to add to my question, the error Xcode was giving me was expected, obvious because I don't know Objective-C, nor Swift, I was trying in my way. Even if I added the error to the question, I'm sure that something else would error too. That's why I asked if someone could convert it for me, it's only a few lines, so I think I was not asking too much.

Thanks @bsod, for mentionating the tool you posted in the comment.. This is exactly the tool I needed. I didn't know that something like this tool existed. 这就是我需要的

Thanks for everyone that helped me.

func application(_ application: UIApplication, 
           didReceive notification: UILocalNotification)

But this method is deprecated. You might want to consider migrating to the "UNUserNotificationCenter" related APIs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM