如何将objective c AppDelegate转换为swift?

时间:2023-01-16 12:05:38

I am working on old project now we are converting all classes to swift, how can I convert AppDelegate class and integration of that class,I need to change any Project settings or main.m .

我正在做旧的项目现在我们正在把所有的类转换成swift,我如何转换AppDelegate类和那个类的集成,我需要改变任何项目设置或main。m。

4 个解决方案

#1


39  

A good starting point is to create a new Swift-Project to get the Template for the AppDelegate, or just copy the following code in your AppDelegate.swift class:

一个好的起点是创建一个新的斯威夫特项目来获取AppDelegate的模板,或者在AppDelegate中复制以下代码。斯威夫特类:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}

In Swift the main.m and AppDelegate class have been merged by using the @UIApplicationMain annotation. Therefore main.m is not required any longer. It's also not required to change your Project setting, hence the @UIApplicationMain will do the work for you. Just be sure to set the correct Target Membership of your AppDelegate class if you have more build targets with different AppDelegates.

在斯威夫特的主要。m和AppDelegate类通过使用@UIApplicationMain注释被合并。因此主要。m不再被要求了。也不需要更改项目设置,因此@UIApplicationMain将为您完成工作。如果您有更多具有不同AppDelegate的构建目标,那么一定要设置正确的AppDelegate类的目标成员。

#2


10  

1) Create a new file in Xcode (File > New > File…) and select a Cocoa Touch Class. Call it AppDelegate, make it a subclass of UIResponder and change the language to Swift.

1)在Xcode中创建一个新文件(file > new > file…)并选择Cocoa Touch类。调用它AppDelegate,使它成为UIResponder的一个子类,并将语言更改为Swift。

2) Fill the AppDelegate.swift file with

2)填充在AppDelegate。快速文件

import UIKit

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

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:     NSDictionary?) -> Bool {
        // Override point for customization after application launch.

        return true
    }
}

3) Remove your main.m and main.h files.

3)删除你的主。m和主要。h文件。

It's done!

这是完成了!

Source: http://www.binpress.com/tutorial/converting-an-objective-c-app-to-swift/118

来源:http://www.binpress.com/tutorial/converting-an-objective-c-app-to-swift/118

#3


4  

In order to improve @seeya answer

为了改进@seeya的答案

SWIFT 3

斯威夫特3

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

}

#4


1  

None of previous answers worked for me, after some researches, this worked:

之前的答案对我都不起作用,经过一些研究后,我发现:

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_BY_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
  }
}

#1


39  

A good starting point is to create a new Swift-Project to get the Template for the AppDelegate, or just copy the following code in your AppDelegate.swift class:

一个好的起点是创建一个新的斯威夫特项目来获取AppDelegate的模板,或者在AppDelegate中复制以下代码。斯威夫特类:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}

In Swift the main.m and AppDelegate class have been merged by using the @UIApplicationMain annotation. Therefore main.m is not required any longer. It's also not required to change your Project setting, hence the @UIApplicationMain will do the work for you. Just be sure to set the correct Target Membership of your AppDelegate class if you have more build targets with different AppDelegates.

在斯威夫特的主要。m和AppDelegate类通过使用@UIApplicationMain注释被合并。因此主要。m不再被要求了。也不需要更改项目设置,因此@UIApplicationMain将为您完成工作。如果您有更多具有不同AppDelegate的构建目标,那么一定要设置正确的AppDelegate类的目标成员。

#2


10  

1) Create a new file in Xcode (File > New > File…) and select a Cocoa Touch Class. Call it AppDelegate, make it a subclass of UIResponder and change the language to Swift.

1)在Xcode中创建一个新文件(file > new > file…)并选择Cocoa Touch类。调用它AppDelegate,使它成为UIResponder的一个子类,并将语言更改为Swift。

2) Fill the AppDelegate.swift file with

2)填充在AppDelegate。快速文件

import UIKit

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

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:     NSDictionary?) -> Bool {
        // Override point for customization after application launch.

        return true
    }
}

3) Remove your main.m and main.h files.

3)删除你的主。m和主要。h文件。

It's done!

这是完成了!

Source: http://www.binpress.com/tutorial/converting-an-objective-c-app-to-swift/118

来源:http://www.binpress.com/tutorial/converting-an-objective-c-app-to-swift/118

#3


4  

In order to improve @seeya answer

为了改进@seeya的答案

SWIFT 3

斯威夫特3

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

}

#4


1  

None of previous answers worked for me, after some researches, this worked:

之前的答案对我都不起作用,经过一些研究后,我发现:

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_BY_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
  }
}