How does AppDelegate.swift replace AppDelegate.h and AppDelegate.m in Xcode 6.3

血红的双手。 提交于 2019-12-07 02:15:37

问题


According to the iOS Developer Library

The app delegate is where you write your custom app-level code. Like all classes, the AppDelegate class is defined in two source code files in your app: in the interface file, AppDelegate.h, and in the implementation file, AppDelegate.m.

However, in Xcode 6.3 it appears that there is only AppDelegate.swift and no longer the .h and .m extensions. I would like to understand how the .swift replaced both the .h and .m extensions.


回答1:


The simple answer is that AppDelegate.swift is just the translation from Objective-C AppDelegate.h and AppDelegate.m, as Swift does not require separate headers and implementations, but rather a single .swift file.

However, under the hood, there are other key differences between the two.

In Objective-C, there exists a main.m file that's sole purpose is instantiating UIApplicationMain

In Swift, the @UIApplicationMain annotation tag found at the top of AppDelegate.swift replaces the need for any main function that existed in the main.m file in Objective-C. If this tag is omitted, you can use a main.swift file to instantiate your UIApplication using the specified App Delegate.

A main.swift implementation looks like this:

import UIKit

autoreleasepool {
    UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))
}



回答2:


Swift is designed to do away with a lot of the code duplication encountered in other languages. Header files can be an easy way to view the interface for code but they largely duplicate the implementation in addition to causing headaches in mismatches between the two. In Swift both the interface and the implementation are one and the same, in the same file.




回答3:


Object-C use to have files .h and .m, now in swift it is not the case anymore, swift does not need header files (.h) and all the code is in the .swift file.

The tutorial in the link you post relates to an Object-C project and not a swift project.

Maybe this document is a better starting point for you if you want to follow that steps in swift




回答4:


If you work with Swift language and if you selected this language when making the project, you will have only one Swift file and nothing else for delegate.

If you select Objective-C, then you will have two separate files, H and M like previous Xcode versions (they didn't have Swift)

The difference is:

In Objective-C, one file is for declarations (Header File | .H) and one for implementations (Main File | .M).

In the new Swift language, the declarations and implementations are in the same file (Swift File | .swift), so Swift made this structure better.

Actually, there is no difference at runtime between these two languages at delegate, but Swift is more easy to learn.

For more info about Swift, download this book from iBooks Store:

The Swift Programming Language (Apple Inc.)




回答5:


Under Swift 3 Xcode 8.3 i am using like this

import UIKit

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


来源:https://stackoverflow.com/questions/30388064/how-does-appdelegate-swift-replace-appdelegate-h-and-appdelegate-m-in-xcode-6-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!