gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:11:07 24 4
gpt4 key购买 nike

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

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/

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