在for-in循环中输入类型 [英] Type casting in for-in loop

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

问题描述

我有这个for-in循环:

 在view.subviews {
}

现在我希望将按钮转换为自定义类,以便使用它的属性。
$ b

我试过这个: for view.subviews中的按钮作为AClass



但是它不工作,并给我一个错误:'AClass'不符合协议'SequenceType'



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



但是这两个都不行。 b $ b

解决方案

对于 Swift 2 及其后续版本:

循环添加了个案模板,这使得在 for 循环中输入cast更简单更安全:

 用于让按钮作为AClass在view.subviews {
//用按钮
做某事}

为什么这比你在 Swift 1.2 和更早?因为个案模式允许您从集合中选取特定的类型。它只匹配你正在寻找的类型,所以如果你的数组包含混合,你可以只对一个特定类型进行操作。



例如:

  let array:[Any] = [1,1.2,Hello,true,[1,2,3],World!] 
用于数组{b $ b print(str)
}

输出:


 您好
世界!







在这种情况下,你正在铸造 view.subviews 而不是 code> button ,所以你需要将它下注到你想要的类型的数组:

 按钮在view.subviews中为! [AClass] {
//使用按钮
做些事情}

如果底层数组类型不是 [AClass] ,则会崩溃。这就是 as!上的正在告诉你的事情。如果您不确定类型,可以使用条件转换作为?以及可选的绑定如果让>:

 如果让subviews = view.subviews为? [AClass] {
//如果我们到达这里,子视图中的按钮类型为[AClass]
{
//按钮
做某事}
}

对于 Swift 1.1 及更早版本:



pre $ 在view.subviews中的按钮作为[AClass] {
//用按钮
做某事}
< code


注意:如果子视图不是全部类型 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天全站免登陆