iOS:在Swift中,如何在测试前检查obj.value(forKey:“key”)是否为零?

时间:2017-11-08 14:17:10

标签: swift

我有一段有效的代码,

var loginFieldText = ""
if let wd = UIApplication.shared.delegate?.window
{
    let vc = wd!.rootViewController //Check
    if(vc is UINavigationController)
    {
        let viewControllers = (vc as! UINavigationController).viewControllers
        for obj in viewControllers {
            if let loginField = obj.value(forKey: "loginField")
            {
                if let loginText = (loginField as AnyObject).value(forKey: "text")
                {
                    loginFieldText = loginText as! String
                }
            }
        }
    }
}

但我的问题是,如果我使用该行,我会收到一条错误消息:

if let loginField = obj.value(forKey: "loginField") 

因为它检查视图中是否存在密钥“密钥”。如何在崩溃之前检查该密钥是否存在?

错误讯息:

  

valueForUndefinedKey:]:此类不是密钥loginField的密码值编码。'

有关信息,如果我尝试使用最合乎逻辑的以下代码:

let loginVC = obj as! LoginViewController 
let loginTF = loginVC.loginTextField  

=>我有一条错误消息:

fatal error: file '/Users/OlostA/Desktop/Git/FormBox/formbox/Formbox/Planning/Formbox-Bridging-Header.h' has been modified since the precompiled header '/Users/OlostA/Library/Developer/Xcode/DerivedData/Formbox-fpnftywlyjuvvubjjzpknxxdyhul/Build/Intermediates.noindex/PrecompiledHeaders/Formbox-Bridging-Header-swift_7N984CYB20BK-clang_28VAG4OSP9DZS.pch' was built
note: please rebuild precompiled header '/Users/OlostA/Library/Developer/Xcode/DerivedData/Formbox-fpnftywlyjuvvubjjzpknxxdyhul/Build/Intermediates.noindex/PrecompiledHeaders/Formbox-Bridging-Header-swift_7N984CYB20BK-clang_28VAG4OSP9DZS.pch'
/Users/OlostA/Desktop/Git/FormBox/formbox/Formbox/Planning/Formbox-Bridging-Header.h:36:9: note: in file included from /Users/OlostA/Desktop/Git/FormBox/formbox/Formbox/Planning/Formbox-Bridging-Header.h:36:
#import "LoginViewController.h"
        ^
/Users/OlostA/Desktop/Git/FormBox/formbox/Formbox/LoginViewController.h:18:9: note: in file included from /Users/OlostA/Desktop/Git/FormBox/formbox/Formbox/LoginViewController.h:18:
#import "MainViewController.h"
        ^
/Users/OlostA/Desktop/Git/FormBox/formbox/Formbox/MainViewController.h:17:9: error: 'Formbox-Swift.h' file not found
#import "Formbox-Swift.h"
        ^
1 error generated.
<unknown>:0: error: failed to emit precompiled header '/Users/OlostA/Library/Developer/Xcode/DerivedData/Formbox-fpnftywlyjuvvubjjzpknxxdyhul/Build/Intermediates.noindex/PrecompiledHeaders/Formbox-Bridging-Header-swift_7N984CYB20BK-clang_28VAG4OSP9DZS.pch' for bridging header '/Users/OlostA/Desktop/Git/FormBox/formbox/Formbox/Planning/Formbox-Bridging-Header.h'

1 个答案:

答案 0 :(得分:0)

首先,请转到产品菜单,然后按选项键。这将改变菜单选项Clean to Clean Build Folder ...这应该可以解决你一直看到的构建错误。

确保桥接标题中包含LoginViewController.h(看起来好像比抱歉更安全)。

然后让我们清理那里你遇到的那个厄运金字塔(别担心,我们都至少写了几个):

var loginFieldText = ""
// Check if the root view controller of the window is a navigation controller and grab a reference if it is.
guard let vc = UIApplication.shared.delegate?.window.rootViewController as? UINavigationController else { return }
let viewControllers = vc.viewControllers
for obj in viewControllers {
    // See if the obj can be cast as a LoginViewController and if the loginField can be retrieved
    guard let loginField = (obj as? LoginViewController)?.loginField else { continue }
    // We want loginFieldText to be an empty string or the contents of loginField so we use the nil-coalescing operator
    loginFieldText = loginField.text ?? loginFieldText
}

我在没有编译器的情况下编写了这个,所以可能会有一些清理,但想法就在那里。