使用Swift子类UIApplication [英] Subclass UIApplication with Swift

查看:238
本文介绍了使用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."

所以,如何子类UIApplication在swift?任何建议?

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

推荐答案

好的,我找到了解决方案
首先,我注意到,顶部的AppDelegate.swift文件,有这一行

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

@UIApplicationMain

由于这一行在任何范围之外(它在文件级),它立即执行,我假设编译器在标准主函数中翻译它。

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 .

import Foundation
import UIKit

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

为UIApplication子类FLApplication.swift创建一个swift文件,代码:

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)
        println("send event") // this is an example
        // ... dispatch the message...
    }
}

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

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

EDIT

正如胡俊峰所说,现在关于 UIApplicationMain 和main.swift文件的解释在Swift语言参考的Attributes部分中有说明: a href =https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html>链接

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

EDIT - 2015年3月

由Thomas Verbeek评论
在XCode 6.3测试版中,您可能会发现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))

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

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