ios - IBAction 上的键值编码合规性错误

标签 ios swift swift3 nsunknownkeyexception anyobject

免责声明:请忽略使用此功能的生产应用可能会或可能不会通过审核的事实。我只是想让这个概念发挥作用。这是我尝试执行的代码,我(目前)尝试做的只是初始化四个常量,第三个常量有困难。

let app = UIApplication.shared
let statusBar = app.value(forKey: "statusBar") as AnyObject
let foregroundView = statusBar.value(forKey: "foregroundView") as AnyObject
let subviews = Array(foregroundView.subviews)

报错:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_SwiftValue 0x170087850> valueForUndefinedKey:]: this class is not key value coding-compliant for the key foregroundView.'

请注意,与应用程序运行时发生的大多数其他键值合规性错误不同,我的错误仅在执行包含四行代码的 IBAction(由 UIButton 触发)时发生。

作为引用,我正在尝试实现什么 this post在 Objective-C 中描述。我是 Swift 的新手,但我认为我翻译得很好。

完整的 func 在这里:

@IBAction func getSignal(_ sender: UIButton) {

    let app = UIApplication.shared
    let statusBar = app.value(forKey: "statusBar") as AnyObject
    let foregroundView = statusBar.value(forKey: "foregroundView") as AnyObject
    let subviews = Array(foregroundView.subviews)
    var dataNetworkItemView: UIView?
    for subview in subviews {
        if (subview as AnyObject).isKind(of: NSClassFromString("UIStatusBarSignalStrengthItemView")!) {
            dataNetworkItemView = subview
            break
        }
    }
    let signalStrength = (dataNetworkItemView?.value(forKey: "signalStrengthRaw") as? NSString)?.intValue
    print("signal \(signalStrength)")
}

我从这里的原始代码块转换而来:

    UIApplication *app = [UIApplication sharedApplication];
    NSArray *subviews = [[[app valueForKey:@"statusBar"]     valueForKey:@"foregroundView"] subviews];
    NSString *dataNetworkItemView = nil;
    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]])
        {
            dataNetworkItemView = subview;
            break;
        }
    }
    int signalStrength = [[dataNetworkItemView valueForKey:@"signalStrengthRaw"] intValue];
    NSLog(@"signal %d", signalStrength);

没有编译时错误或警告,我不知道如何继续。谢谢。

最佳答案

工作 Xcode 8 beta,Swift 3.0 代码:

let app = UIApplication.shared
let statusBar = app().value(forKey: "statusBar")! as AnyObject
let foregroundView = statusBar.value(forKey: "foregroundView")! as AnyObject
let subviews = Array(foregroundView.subviews)
var dataNetworkItemView: UIView?
for subview in subviews {
    if (subview as AnyObject).isKind(of: NSClassFromString("UIStatusBarSignalStrengthItemView")!) {
        dataNetworkItemView = subview
        break
    }
}
let signalStrength = (dataNetworkItemView?.value(forKey: "signalStrengthRaw") as? NSString)?.intValue
print("signal \(signalStrength)")

OP 使用的是 Xcode 8 GM,因此有一个变化 - app()app:

let statusBar = app.value(forKey: "statusBar")! as AnyObject

关于ios - IBAction 上的键值编码合规性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39542848/

相关文章:

ios - 使导航堆栈中只有一个导航栏透明 iOS Swift

iphone - iOS UI 自动化 : is it possible to compare screenshots to reference images?

ios - 如何控制多个同名的 Sprite 套件节点?

ios - 呈现 UIAlertViewController 抛出 "View not in window heirarchy"- Swift

ios - 使用 swift 在 ios 中的 textview 中嵌入图像

ios - swift 3 错误 : "Binary operator ' /' cannot be applied to two ' int' operands"

swift - Sinch swift 3 短信验证

ios - 带有 UINavigationController 作为选项卡的 UITabViewController

ios - 可选类型的默认值 'URL?'

ios - 如何有效地解析 mpeg 文件?