【问题标题】:How to convert Objective-C App Delegate to Swift?如何将 Objective-C 应用程序委托转换为 Swift?
【发布时间】:2015-09-27 08:12:58
【问题描述】:

我正在处理将所有类转换为 Swift 的旧项目。

如何将AppDelegate 类转换为 Swift?

我必须在项目设置或main.m 中进行哪些更改?

【问题讨论】:

  • 几个步骤,但很容易做到。我在办公室有我的脚本,如果你明天之前没有答案,我会发布它。
  • 但是你需要在swift文件的顶部添加@main之类的东西,并去掉main.m和main.h文件
  • 你的 main 应该是 main.swift
  • @Aggressor 谢谢我会试试的

标签: ios objective-c swift appdelegate


【解决方案1】:

一个好的起点是创建一个新的 Swift-Project 以获取 AppDelegate 的模板,或者只需将以下代码复制到您的 AppDelegate.swift 类中:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: 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.
    }
}

在 Swift 中,main.mAppDelegate 类已通过使用 @UIApplicationMain 注释进行合并。因此main.m 不再需要,应该从您的项目中删除。

也不需要更改您的项目设置,因此@UIApplicationMain 将为您完成工作。如果您有更多具有不同 AppDelegates 的构建目标,请务必为您的 AppDelegate 类设置正确的 Target Membership。

【讨论】:

  • @seeya 你的意思是我们可以删除main.m吗?
  • @aircraft 是的,你可以
  • SceneDelegate.swift 怎么样?白色启动屏幕后屏幕变黑。
【解决方案2】:
  1. 在 Xcode 中创建一个新文件(文件 > 新建 > 文件...)并选择一个 Cocoa Touch 类。将其命名为 AppDelegate,使其成为 UIResponder 的子类并将语言更改为 Swift。

  2. 在 AppDelegate.swift 文件中填写:

    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. 删除您的 main.m 和 main.h 文件。

完成了!

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

【讨论】:

  • 感谢您的回答,这是转换 appDelegate 类的好方法
  • @Fawad Masud 我的项目没有main.h,如果我删除main.m,就会报error
  • @aircraft 也删除 main.m 并编译项目。
  • @Fawad Masud 你对,我这样做了,现在它变得正常了。
【解决方案3】:

为了改进@seeya answer

SWIFT 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:.
    }

}

【讨论】:

  • @Vyacheslav 我应该删除 AppDelegate.hAppDelegate.m 吗?但在main.m 中需要AppDelegate.h
  • @aircraft。 Yiu 不需要更多的 main.m 文件。你必须删除。 uiapplicationmain word 和 main.m 一样
  • 你必须删除所有这些文件
  • @Vyacheslav 你说的对,之前会报错,但是现在我清理我的项目并再次运行,现在没有这个问题了。
【解决方案4】:

以前的答案都没有对我有用,经过一些研究,这有效:

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

【讨论】:

    【解决方案5】:

    粘贴上述答案的解决方案会导致 Xcode 建议更新函数声明以将其标记为 private。在这种情况下,该函数将不会被系统调用,并且您的应用永远不会启动。

    斯威夫特 5

    如下使用AppDelegate.swift

    import UIKit
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
        
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            let rootViewController = UIViewController() // replace with your view controller
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.rootViewController = rootViewController
            window?.makeKeyAndVisible()
            return true
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多