使用 Swift 继承 UIApplication [英] Subclass UIApplication with Swift

查看:23
本文介绍了使用 Swift 继承 UIApplication的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Objective C 中很简单:更新 main.m 文件并更改 UIApplicationMain() 参数就足够了

In Objective C it was simple: it was sufficient to update the main.m file and change the UIApplicationMain() parameters

return UIApplicationMain(argc, argv, NSStringFromClass([CustomUIApplication class]), NSStringFromClass([AppDelegate class]));

但是在 swift 中没有 main.m 文件,因为指南说

But in swift there is no main.m file, since the guide says

在全局范围内编写的代码用作程序的入口点,因此您不需要主函数."

"Code written at global scope is used as the entry point for the program, so you don’t need a main function."

那么,如何在 swift 中继承 UIApplication??有什么建议吗?

So, how to subclass UIApplication in swift?? Any suggestion?

推荐答案

注意 XCode 10.1 和 Swift 5 的语法已于 2019 年 6 月更新(归功于 这里是马特的回答 && Tung Fam 的回答在这里 ),如果您正在寻找以前的语法,请查看编辑部分.

NOTE the syntax has been updated for XCode 10.1 and Swift 5 in Jun 2019 ( credits to matt's answer here && Tung Fam's answer here ), if you are looking for the previous syntaxes look at the edit section.

好的,我找到了解决方案

Ok, I've found the solution

首先,我注意到,在 AppDelegate.swift 文件的顶部,有一行

First, I've noticed that, at the top of the AppDelegate.swift file, there is this line

@UIApplicationMain

由于这一行在任何范围之外(它在文件级别),它会立即执行,并且我假设编译器将其转换为标准的 main 函数.

Since this line is outside any scope (it's at file level), it's executed immediately, and I assume that the compiler translate it in a standard main function.

所以,我这样做了,从一个新的 Swift-Only 应用程序开始:

So, I did this, starting from a new Swift-Only application:

  • 注释掉@UIApplicationMain
  • 添加了一个像这样的 main.swift 文件(FLApplication 是我的子类).
    重要 文件必须命名为 main.swift,因为其他文件不支持顶级语句!您不能在任何其他文件中添加 UIApplicationMain() 调用,否则您将收到此错误:
  • commented out @UIApplicationMain
  • added a main.swift file like this (FLApplication is my subclass).
    IMPORTANT the file MUST BE NAMED main.swift, since top level statements are not supported on other files! You can't add the UIApplicationMain() call inside any other file, otherwise you'll receive this error:

不允许在顶层使用表达式

Expressions are not allowed at the top level

这是 main.swift 文件

This is the main.swift file

UIApplicationMain(
    CommandLine.argc, CommandLine.unsafeArgv, 
    NSStringFromClass(FLApplication.self), NSStringFromClass(AppDelegate.self)
)

然后,使用以下代码为 UIApplication 子类 FLApplication.swift 创建一个 swift 文件:

Then, create a swift file for the UIApplication subclass, FLApplication.swift, with this code:

import UIKit
import Foundation

class FLApplication: UIApplication {
    override func sendEvent(_ event: UIEvent) {
        super.sendEvent(event)
        print("send event")
    }
}

现在,UIApplication 已正确子类化,您将在日志中看到发送事件"消息

now, UIApplication is correctly subclassed and you'll see the "send event" messages in the log

旧编辑
作为参考,由于从版本 1 到版本 3 发生了很大变化,所以我把之前的所有编辑都留在这里

OLD EDITS
For reference, since this has changed a lot from version 1 to version 3, I leave here all the previous edits

<小时>

编辑 - 2015 年 3 月

正如胡俊峰所评论的,关于 UIApplicationMain 和 main.swift 文件的解释记录在 The Swift Language Reference 的 Attributes 部分:链接

As commented by Hu Junfeng now the explanations about UIApplicationMain and the main.swift file are documented in the Attributes section of The Swift Language Reference: Link

正如 Thomas Verbeek 评论的那样在 XCode 6.3 Beta 中,您可能会发现 C_ARGC 和 C_ARGV 已分别重命名为 Process.argc 和 Process.unsafeArgv.您在 main.swift 文件中的 UIApplicationMain 调用需要更新为:

As commented by Thomas Verbeek In the XCode 6.3 Beta, you might find that C_ARGC and C_ARGV have been renamed to Process.argc and Process.unsafeArgv respectively. Your UIApplicationMain call in the main.swift file will need updating to:

UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(FLApplication), NSStringFromClass(AppDelegate))

XCode 8 之前的语法是

The pre-XCode 8 syntax was

import Foundation
import UIKit

UIApplicationMain(C_ARGC, C_ARGV, NSStringFromClass(FLApplication), NSStringFromClass(AppDelegate))

<小时>

编辑 - 2016 年 12 月

Xcode 8,beta 6 之前的解决方案

Solution for Xcode 8, before beta 6

import Foundation
import UIKit

UIApplicationMain(
    CommandLine.argc,
    UnsafeMutableRawPointer(CommandLine.unsafeArgv)
        .bindMemory( 
            to: UnsafeMutablePointer<Int8>.self, 
            capacity: Int(CommandLine.argc)),
    NSStringFromClass(FLApplication.self),
    NSStringFromClass(AppDelegate.self)
)

这篇关于使用 Swift 继承 UIApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆