Swap out App Delegate objective-c file and replace with swift App delegate?

时间:2016-04-15 14:55:14

标签: ios objective-c swift delegates

I am looking to replace my Objective-c App delegate from to a Swift App-Delegate. I have converted a great deal of code already in Swift, looking to finally covert the App-Delegate. I've been researching around the internet and haven't found any really concrete information to go about achieving this so I'd figure it might be helpful to create a StackOverFlow question regarding the topic. Looks like I can create a new app delegate from the File-> New file menu in X-code. But my question is how does the complier know to pick the Swift App-Delegate VS Objective-C App-Delegate? I've looked around in the build settings, haven't really discovered anything that would be pointing to this delegate (Other than the build phases). Any help would be greatly appreciated. Thanks!

2 个答案:

答案 0 :(得分:1)

Assuming this project was constructed as an Objective-C project, you should have a main.m file that includes a call to UIApplicationMain. The final parameter in that call specifies the application delegate class name.

答案 1 :(得分:0)

In Obj-C file main.m is the enter point to a program. In this file you tell which class will be the app delegate by calling UIApplicationMain function:

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

In Swift you can use @UIApplicationMain attribute for your AppDelegate class to indicate enter point and delegate class. Or you can create main.swift file.

You can't have both main.m and main.swift (or app delegate class with @UIApplicationMain attribute) in same project. In this case you will get linker error.

So to switch to Swift implementation of your app delegate you need mark it with attribute, then remove old Obj-C class and main.m file. Or you need to specify new class in main.m file.