Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace AppDelegate.h and AppDelegate.m to AppDelegate.swift

I just have a Objective-C project and I want to use AppDelegate to access to my xcdatamodel, but when I add AppDelegate.swift to my project it show me a compile problem, it is error because it doesn't know what AppDelegate use and what is the main. Then I tried to delete AppDelegate.h and AppDelegate.m and add AppDelegate.swift, but it also doesn't work and still has compile errors. How I can add AppDelegate.swift to it?

like image 774
Max Max Avatar asked Jun 02 '15 12:06

Max Max


People also ask

How to create an appdelegate in SwiftUI?

Lets start with the Adding the swiftUI AppDelegate , Initially create a AppDelegate. swift file func application ( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [ UIApplication. LaunchOptionsKey: Any ]?) -> Bool { // Override point for customization after application launch.

How to add appdelegate in app in Xcode?

Xcode has come up with the App instead of AppDelegate. We can add the AppDelegate in the App using the following method Known as Lets start with the Adding the swiftUI AppDelegate , Initially create a AppDelegate. swift file

What is an app delegate object?

Your app delegate object manages your app’s shared behaviors. The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app’s launch cycle so it is always present.


3 Answers

If you want to use AppDelegate.swift instead of AppDelegate.h / AppDelegate.m, you should add the Swift file to your project and delete the .h and .m files (don't forget to click yes when asked about the bridging header). In addition, you should also delete main.m since it is not needed anymore and will cause a linker error.

like image 89
Teo Avatar answered Oct 09 '22 23:10

Teo


Teo is correct. To solve the main.m file compile problem add @UIApplicationMain above the class declaration

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 
like image 45
Thimon Avatar answered Oct 10 '22 00:10

Thimon


I recommend you to create a new Xcode project with Swift as default and check out the AppDelegate code in that one and copy/adapt it over to your project.

like image 1
BadmintonCat Avatar answered Oct 09 '22 23:10

BadmintonCat