8

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.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Mark B
  • 147
  • 2
  • 8

5 Answers5

10

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))
}
Ian
  • 12,538
  • 5
  • 43
  • 62
  • Hmm, how would one register this main.swift with Xcode for compilation? – TealShift Apr 11 '18 at 17:42
  • Ahh, "Process" should now be "CommandLine", but CommandLine.unsafeArgv has a slightly different type from what UIApplicationMain expects for argument 2. :/ – TealShift Apr 11 '18 at 18:16
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.

0

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

Icaro
  • 14,585
  • 6
  • 60
  • 75
0

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.)

Seyed Parsa Neshaei
  • 3,470
  • 18
  • 30
0

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)
)
user1039695
  • 981
  • 11
  • 20