gpt4 book ai didi

ios - Swift isKindOfClass 不起作用

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

我有一个 ViewController 和一些供用户输入的标签和文本字段,在某些时候我需要遍历所有文本字段以收集用户的输入。

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
for views in self.view.subviews {
if view is UITextField
{
println("inside")
}
println(views.description)

}
}

对于此代码,“内部”永远不会打印出来。但是我确实看到了一些views.description的信息

   <UILabel: 0x7fa4b1c7bd00; frame = (16 97; 76 21); text = 'FromDate';        opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer =           <_UILabelLayer: 0x7fa4b1c44580>>
<UILabel: 0x7fa4b1c9fd60; frame = (16 169; 82 21); text = 'ControlNO'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa4b1c77fb0>>

>

   <UITextField: 0x7fa4b1c7a050; frame = (106 95; 197 30); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7fa4b1c6c800>>

<UITextField: 0x7fa4b1c9c060; frame = (106 126; 197 30); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; tag = 1; layer = <CALayer: 0x7fa4b1c9c2c0>>

<_UILayoutGuide: 0x7fa4b1d11d20; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fa4b1d11e00>>
<_UILayoutGuide: 0x7fa4b1d253f0; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fa4b1d12f60>>

但是如果我 println(views.dynamicType) 我只能看到 ExistentialMetatype。

我的代码有什么问题?

最佳答案

循环变量叫做views,但是你测试view(这是不是循环变量,因此等同于 self.view):

for views in self.view.subviews { // "views" here
if view is UITextField { // "view" here
println("inside")
}
}

如果将循环变量重命名为view

for view in self.view.subviews {
if view is UITextField {
println("inside")
}
}

然后一切都按预期工作。

关于ios - Swift isKindOfClass 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29976686/

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