作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个接收 Set<NSObject>
的函数我需要将集合迭代为 Set<UITouch>
.我该如何测试这个并打开集合?
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches {
// ...
}
}
最佳答案
通常您会使用条件转换来检查每个元素对于它的类型。但是在这里,touches
参数是 documented 作为
A set of
UITouch
instances that represent the touches that are moving during the event represented by event.
因此你可以强制施放整个集合:
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches as! Set<UITouch> {
// ...
}
}
请注意,在 Swift 2 中,函数声明更改为
func touchesMoved(_ touches: Set<UITouch>, withEvent event: UIEvent?)
(由于 Objective-C 中的“轻量级泛型”)因此不再需要强制转换。
关于swift - 遍历设置为某种类型的 NSObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31365720/
我是一名优秀的程序员,十分优秀!