Swift:无法将AnyObject下传到SKPhysicsBody [英] Swift: Unable to downcast AnyObject to SKPhysicsBody

查看:171
本文介绍了Swift:无法将AnyObject下传到SKPhysicsBody的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple在SKPhysicsBody类中有以下方法。

Apple has the following method in the SKPhysicsBody class.

 /* Returns an array of all SKPhysicsBodies currently in contact with this one */
    func allContactedBodies() -> [AnyObject]!



我注意到它返回一个AnyObject数组。因此,我了解了如何处理AnyObject 此处

我想循环通过我的物理体的allContactedBodies数组。问题是,无论我尝试什么,我只是不能让事情工作。

I want to loop through the allContactedBodies array of my physics body. The problem is, no matter what I try I just can't get things to work.

我试过这个:

for body in self.physicsBody.allContactedBodies() as [SKPhysicsBody] {

}

但我收到此错误。


致命错误:array不能downcast到派生数组

fatal error: array cannot be downcast to array of derived

我也尝试过:

for object in self.physicsBody.allContactedBodies()  {
    let body = object as SKPhysicsBody
}

但这也会导致以下错误:

But this also crashes with the following:

同样,我尝试了:

 for object in self.physicsBody.allContactedBodies()  {
     let body = object as? SKPhysicsBody

 }

没有崩溃, 。

如果我不施放,我就不会崩溃。例如:

And if I don't cast at all, I don't get a crash. For example:

for object in self.physicsBody.allContactedBodies()  {

}

但是显然我需要转换,如果我想使用实际类型。

But obviously I need to cast if I want to use the actual type.


那么作为测试,我只是尝试了:

So then as a test I just tried this:

let object: AnyObject = SKPhysicsBody()
let body = object as SKPhysicsBody

这也导致图片中的同一次崩溃。

And this also results in the same crash that is in the picture.


但是其他类型不会崩溃。例如,这不会崩溃。

But other types won't crash. For example, this won't crash.

let object: AnyObject = SKNode()
let node = object as SKNode

所以我的问题是,如何正确循环allContactedBodies数组

编辑:我在iOS 8 beta 4设备上运行Xcode 6 beta 4.

I am running Xcode 6 beta 4 on iOS 8 beta 4 device.

编辑2:更多信息

好的,我只是做了一些更多的测试。我尝试这样:

Ok so I just did some more testing. I tried this:

let bodies = self.physicsBody.allContactedBodies() as? [SKPhysicsBody]

如果allContactedBodies为空,则转换成功。但是如果allContactedBodies包含对象,那么转换失败,bodies将变为nil,因此我无法循环遍历它。看来,目前只是不可能将AnyObject传递给SKPhysicsBody,除非有人能提供解决方法,否则不可能循环遍历allContactedBodies数组。

If "allContactedBodies" is empty, then the cast is successful. But if "allContactedBodies" contains objects, then the cast fails and "bodies" will become nil, so I can't loop through it. It seems that currently it is just NOT POSSIBLE to cast AnyObject to SKPhysicsBody, making it impossible to loop through the "allContactedBodies" array, unless someone can provide a workaround.

3:Bug仍在Xcode 6 beta 5.下面的解决方法仍然有效

编辑4:Bug仍然在Xcode 6 beta 6.下面的解决方法仍然有效

编辑5:失望。 Bug仍然在Xcode 6 GM。解决方法仍然有效

Edit 3: Bug still in Xcode 6 beta 5. Workaround posted below still works
Edit 4: Bug still in Xcode 6 beta 6. Workaround posted below still works
Edit 5: Disappointed. Bug still in Xcode 6 GM. Workaround posted below still works

编辑6:我收到了来自Apple的以下讯息:


工程提供了以下信息:

Engineering has provided the following information:

我们相信这个问题已在最新的Xcode 6.1测试版中解决。

We believe this issue has been addressed in the latest Xcode 6.1 beta.

但是不是,错误仍然在Xcode 6.1.1!解决方法仍然有效。

BUT IT IS NOT, the bug is still in Xcode 6.1.1!!! Workaround still works.

编辑7: Xcode 6.3,仍然不能修复,解决方法仍然有效。

Edit 7: Xcode 6.3, still not fixed, workaround still works.

推荐答案

经过多次尝试和错误,我找到了一个解决方法我的问题。事实证明,当类型是AnyObject时,你不需要向下转换来访问SKPhysicsBody的属性。

After much trial and error, I have found a workaround to my problem. It turns out that you don't need to downcast at all to access the properties of the SKPhysicsBody, when the type is AnyObject.

for object in self.physicsBody.allContactedBodies()  {
        if object.node??.name == "surface" {
            isOnSurface = true
        }
    }

这篇关于Swift:无法将AnyObject下传到SKPhysicsBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆