How AppDelegate.swift replaces AppDelegate.h and AppDelegate.m in Xcode 6.3

According to iOS Developer Library

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

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

+6
source share
5 answers

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

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

There is a main.m file in Objective-C whose sole purpose is to create an instance of UIApplicationMain

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

The implementation of main.swift is as follows:

 import UIKit autoreleasepool {    UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate.self)) } 
+8
source

Swift is designed to eliminate a lot of code duplication found in other languages. Header files can be an easy way to view the interface for the code, but they largely duplicate the implementation in addition to causing headaches in the inconsistencies between them. In Swift, both interfaces and implementation are the same in the same file.

+1
source

Object-C uses .h and .m files, now in swift this is no longer the case, swift does not need header (.h) files, and all the code is in the .swift file.

The tutorial for the link you publish is related to an Object-C project, not a quick project.

This document may be the best starting point for you if you want to follow these steps in a quick

0
source

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

If you choose Objective-C, then you will have two separate files: H and M, like previous versions of Xcode (they did not have Swift)

The difference is as follows:

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

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

Actually, there is no runtime difference between the two languages ​​in the delegate, but Swift is easier to learn.

For more information on Swift, download this book from the iBooks Store:

Fast Programming Language (Apple Inc.)

0
source

In Swift 3 Xcode 8.3 I use like this

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

Source: https://habr.com/ru/post/987713/


All Articles