在 for-in 循环中进行类型转换 [英] Type casting in for-in loop

查看:62
本文介绍了在 for-in 循环中进行类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 for-in 循环:

for view.subviews 中的按钮 {}

现在我想将按钮转换为自定义类,以便我可以使用其属性.

我试过这个:for button in view.subviews as AClass

但它不起作用并给我一个错误:'AClass'不符合协议'SequenceType'

我试过这个:for button:AClass in view.subviews

但这也不起作用.

解决方案

对于 Swift 2 及更高版本:

Swift 2 在 for 循环中添加了 case 模式,这使得在 for 循环中进行类型转换变得更加容易和安全:

 for case let button as AClass in view.subviews {//用按钮做一些事情}

为什么这比您在 Swift 1.2 及更早版本中可以做的更好?因为case patterns 允许您从集合中选择您的特定类型.它只匹配您要查找的类型,因此如果您的数组包含混合,则您只能对特定类型进行操作.

例如:

let array: [Any] = [1, 1.2, "Hello", true, [1, 2, 3], "World!"]对于 case let str as String in array {打印(字符串)}

输出:

<块引用>

你好世界!

<小时>

对于 Swift 1.2:

在这种情况下,您正在转换 view.subviews 而不是 button,因此您需要将其向下转换为您想要的类型的数组:

for view.subviews 中的按钮为![一类] {//用按钮做一些事情}

注意:如果底层数组类型不是[AClass],则会崩溃.这就是 as! 上的 ! 告诉你的.如果您不确定类型,您可以使用条件转换 as? 以及可选绑定 if let:

if let subviews = view.subviews as?[一类] {//如果我们到达这里,则子视图的类型为 [AClass]对于子视图中的按钮 {//用按钮做一些事情}}

对于 Swift 1.1 及更早版本:

for view.subviews 中的按钮为 [AClass] {//用按钮做一些事情}

注意:如果子视图不都是 AClass 类型,这也会崩溃.上面列出的安全方法也适用于早期版本的 Swift.

I have this for-in loop:

for button in view.subviews {
}

Now I want button to be cast into a custom class so I can use its properties.

I tried this: for button in view.subviews as AClass

But it doesnt work and gives me an error:'AClass' does not conform to protocol 'SequenceType'

And I tried this: for button:AClass in view.subviews

But neither does that work.

解决方案

For Swift 2 and later:

Swift 2 adds case patterns to for loops, which makes it even easier and safer to type cast in a for loop:

for case let button as AClass in view.subviews {
    // do something with button
}

Why is this better than what you could do in Swift 1.2 and earlier? Because case patterns allow you to pick your specific type out of the collection. It only matches the type you are looking for, so if your array contains a mixture, you can operate on only a specific type.

For example:

let array: [Any] = [1, 1.2, "Hello", true, [1, 2, 3], "World!"]
for case let str as String in array {
    print(str)
}

Output:

Hello
World!


For Swift 1.2:

In this case, you are casting view.subviews and not button, so you need to downcast it to the array of the type you want:

for button in view.subviews as! [AClass] {
    // do something with button
}

Note: If the underlying array type is not [AClass], this will crash. That is what the ! on as! is telling you. If you're not sure about the type you can use a conditional cast as? along with optional binding if let:

if let subviews = view.subviews as? [AClass] {
    // If we get here, then subviews is of type [AClass]
    for button in subviews {
        // do something with button
    }
}

For Swift 1.1 and earlier:

for button in view.subviews as [AClass] {
    // do something with button
}

Note: This also will crash if the subviews aren't all of type AClass. The safe method listed above also works with earlier versions of Swift.

这篇关于在 for-in 循环中进行类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆