gpt4 book ai didi

ios - 仅在 DEBUG 配置上符合协议(protocol)

转载 作者:搜寻专家 更新时间:2023-10-31 22:16:19 26 4
gpt4 key购买 nike

当设置了 DEBUG 标志时,我试图启用类协议(protocol):

#if DEBUG
class LoginViewController: UIViewController, UITextFieldDelegate {
#else
class LoginViewController: UIViewController {
#endif
//...
}

虽然它没有编译,#else 行上的“Expected declaration”。

最佳答案

swift 中的预处理器指令与您可能习惯使用的不同。

Apple documentation关于这个主题,#if/#else/#endif 语句之间包含的任何内容都必须是有效的 swift 语句。

In contrast with condition compilation statements in the C preprocessor, conditional compilation statements in Swift must completely surround blocks of code that are self-contained and syntactically valid. This is because all Swift code is syntax checked, even when it is not compiled.

因为您的语句是片段(以大括号结尾),所以它们无法编译。

您可能必须执行以下操作才能完成您想要的:

class LoginViewController: UIViewController {
...
}

#if DEBUG
class DebugLoginViewController: LoginViewController, UITextFieldDelegate {
(just add the UITextFieldDelegate code here
let LoginViewController handle the rest)
}
#endif

然后您将使用 #if DEBUG/#else/#endif 实例化您的 View Controller 来选择DebugLoginViewController 与否。

关于ios - 仅在 DEBUG 配置上符合协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28027892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com