xcode 私有函数 didFinishLaunchingWithOptions 没有被调用?(斯威夫特 3)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39888601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:24:15  来源:igfitidea点击:

Private func didFinishLaunchingWithOptions not being called? (Swift 3)

iosswiftxcodelaunch

提问by Freddy Benson

Isn't didFinishLaunchingWithOptionssupposed to be called when the app starts running for the first time? I set a breakpoint at this method and when I run the app in the simulator the breakpoint doesn't get hit, which means the method doesn't get called. I'm trying to load some data from UserDefaults whenever the app launches, but it's being completely ignored. One thing I noticed is that it's by default a private funcinstead of a func. If I get rid of the private, I receive a warning that "there's an almost similar optional requirement in the UIApplicationDelegate". Can someone explain to me what this means and whether or not the private funchas anything to do with the method being ignored? Is that method even supposed to be called when I run my app in the simulator? If not, how can I test if data is being retrieved after my app launches? All the other methods in the AppDelegatedo get called normally (for example, the applicationDidEnterBackgroundmethod works perfectly fine).

didFinishLaunchingWithOptions应用程序第一次开始运行时不应该被调用吗?我在这个方法上设置了一个断点,当我在模拟器中运行应用程序时,断点没有被命中,这意味着该方法没有被调用。每当应用程序启动时,我都试图从 UserDefaults 加载一些数据,但它被完全忽略了。我注意到的一件事是默认情况下它是 aprivate func而不是 a func。如果我摆脱了private,我会收到一条警告“UIApplicationDelegate 中有一个几乎相似的可选要求”。有人可以向我解释这意味着什么以及是否private func与被忽略的方法有什么关系?当我在模拟器中运行我的应用程序时,甚至应该调用该方法吗?如果没有,我如何测试在我的应用程序启动后是否正在检索数据?AppDelegatedo中的所有其他方法都被正常调用(例如,该applicationDidEnterBackground方法工作得很好)。

回答by Yannick

Remove your method signature and have Xcode autocomplete it

删除您的方法签名并让 Xcode 自动完成它

I also had the problem that my didFinishLaunchingWithOptionsmethod in AppDelegate would not be called. My function was also marked private and looked like this

我也遇到了我didFinishLaunchingWithOptions在 AppDelegate 中的方法不会被调用的问题。我的函数也被标记为私有,看起来像这样

private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

The problem is that this is the old syntax! Apparently for me when I converted my project from Swift 2.x to Swift 3 Xcode did not convert the methods in AppDelegate. The new syntax looks like this

问题是这是旧语法!显然,当我将我的项目从 Swift 2.x 转换为 Swift 3 时,Xcode 没有转换 AppDelegate 中的方法。新语法如下所示

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool

Swift 4.2:

斯威夫特 4.2:

func application( _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

回答by Meghs Dhameliya

for Swift ~3.0 Replace didFinishLaunchingWithOptionswith follwing signature

对于 Swift ~3.0用以下签名替换 didFinishLaunchingWithOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
}

回答by Mathews

Have you implemented the didFinishLaunchingWithOptionsin one of your ViewControllers? One will not get a call to the custom implementation of this method. This method is defined in the ApplicationDelegateand it will always be called once the app is launched. If you haven't defined the method again in any ViewControllerand the one in AppDelegateis not being called, then try resetting the simulator. From the simulator menu Simulator -> Reset content and settings.

你有没有在你的didFinishLaunchingWithOptions一个中实现ViewControllers?不会调用此方法的自定义实现。此方法在 中定义,ApplicationDelegate一旦应用程序启动,它将始终被调用。如果您还没有在 any 中再次定义该方法,ViewController并且AppDelegate没有调用in 中的一个,请尝试重置模拟器。从模拟器菜单Simulator -> Reset content and settings

If compiler prompts to make the didFinishLaunchingWithOptionsmethod private then the parameter of the method might be causing the error. The parameter of the application(_:didFinishLaunchingWithOptions:) delegate method is now bridged to Swift as a [UIApplicationLaunchOptionsKey: Any]?, rather than an [NSObject : AnyObject]?. So modify the method signature as shown.

如果编译器提示将该didFinishLaunchingWithOptions方法设为私有,则该方法的参数可能会导致错误。application(_:didFinishLaunchingWithOptions:) 委托方法的参数现在作为 [UIApplicationLaunchOptionsKey: Any]? 桥接到 Swift,而不是 [NSObject : AnyObject]?。所以修改方法签名,如图所示。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ...
}

回答by vishnu varthan

This method is defined in the ApplicationDelegate and it will always be called once the app is launched. If you have not defined the method again in any ViewController and the one in AppDelegate is not being called, then try resetting the simulator.

此方法在 ApplicationDelegate 中定义,一旦应用程序启动,它将始终被调用。如果您没有在任何 ViewController 中再次定义该方法,并且 AppDelegate 中的方法没有被调用,请尝试重置模拟器。

Open simulator - > menu Simulator -> Reset content and settings.

打开模拟器 -> 菜单模拟器 -> 重置内容和设置。

-(BOOL)application(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//..
}

回答by Pham Quan

Update for Swift 4.2:

Swift 4.2 更新:

func application(
    _ application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool

回答by RanLearns

Delete app from device and restart Xcode worked for me here

从设备中删除应用程序并重新启动 Xcode 在这里对我来说有效