在forin循环中输入铸件

时间:2022-12-08 11:43:01

I have this for-in loop:

我有这个forin循环:

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

我尝试了这个:为了查看按钮。子视图作为AClass

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

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

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

我尝试了:for button:AClass in view.subviews

But neither does that work.

但这也不管用。

4 个解决方案

#1


98  

For Swift 2 and later:

Swift 2及以后:

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

Swift 2为for循环添加了case模式,这使得在for循环中输入cast更加容易和安全:

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.

为什么这比你在斯威夫特1.2和更早的时候能做的更好呢?因为case模式允许您从集合中选择特定类型。它只匹配您要查找的类型,所以如果您的数组包含一个混合,那么您只能对一个特定的类型进行操作。

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:

为迅速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:

注意:如果底层数组类型不是[AClass],这将导致崩溃。那是什么!作为!是告诉你。如果您不确定类型,您可以使用条件转换为?与可选绑定一起,如果让:

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:

Swift 1.1及之前版本:

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.

注意:如果子视图不是所有类型的AClass,那么它也会崩溃。上面列出的安全方法也适用于Swift的早期版本。

#2


103  

This option is more secure:

这个选项更安全:

for case let button as AClass in view.subviews {
}

#3


3  

You can also use a where clause

您还可以使用where子句

for button in view.subviews where button is UIButton {
    ...
}

#4


1  

The answer provided by vacawama was correct in Swift 1.0. And no longer works with Swift 2.0.

vacawama给出的答案在Swift 1.0中是正确的。也不再适用于Swift 2.0。

If you try, you will get an error similar to:

如果你尝试,你会得到一个类似于:

'[AnyObject]' is not convertible to '[AClass]';

'[任何对象]'不能转换为'[AClass]';

In Swift 2.0 you need to write like:

在Swift 2.0中,您需要这样写:

for button in view.subviews as! [AClass]
{
}

#1


98  

For Swift 2 and later:

Swift 2及以后:

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

Swift 2为for循环添加了case模式,这使得在for循环中输入cast更加容易和安全:

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.

为什么这比你在斯威夫特1.2和更早的时候能做的更好呢?因为case模式允许您从集合中选择特定类型。它只匹配您要查找的类型,所以如果您的数组包含一个混合,那么您只能对一个特定的类型进行操作。

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:

为迅速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:

注意:如果底层数组类型不是[AClass],这将导致崩溃。那是什么!作为!是告诉你。如果您不确定类型,您可以使用条件转换为?与可选绑定一起,如果让:

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:

Swift 1.1及之前版本:

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.

注意:如果子视图不是所有类型的AClass,那么它也会崩溃。上面列出的安全方法也适用于Swift的早期版本。

#2


103  

This option is more secure:

这个选项更安全:

for case let button as AClass in view.subviews {
}

#3


3  

You can also use a where clause

您还可以使用where子句

for button in view.subviews where button is UIButton {
    ...
}

#4


1  

The answer provided by vacawama was correct in Swift 1.0. And no longer works with Swift 2.0.

vacawama给出的答案在Swift 1.0中是正确的。也不再适用于Swift 2.0。

If you try, you will get an error similar to:

如果你尝试,你会得到一个类似于:

'[AnyObject]' is not convertible to '[AClass]';

'[任何对象]'不能转换为'[AClass]';

In Swift 2.0 you need to write like:

在Swift 2.0中,您需要这样写:

for button in view.subviews as! [AClass]
{
}