Type casting in for-in loop

后端 未结 6 1657

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.

6条回答
  •  长情又很酷
    2020-12-07 20:01

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

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

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

    In Swift 2.0 you need to write like:

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

提交回复
热议问题