gpt4 book ai didi

swift - 遍历设置为某种类型的 NSObject

转载 作者:搜寻专家 更新时间:2023-11-01 06:08:23 24 4
gpt4 key购买 nike

我有一个接收 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com