斯威夫特:无法垂头丧气AnyObject到SKPhysicsBody [英] Swift: Unable to downcast AnyObject to SKPhysicsBody

查看:107
本文介绍了斯威夫特:无法垂头丧气AnyObject到SKPhysicsBody的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

苹果已经在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 <一个href=\"https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CDQQygQwAw&url=https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2F$p$prelease%2Fmac%2Fdocumentation%2FSwift%2FConceptual%2FSwift_Programming_Language%2FTypeCasting.html%23%2F%2Fapple_ref%2Fdoc%2Fuid%2FTP40014097-CH22-XID_492&ei=SLDWU__sNo7hsASKp4HACQ&usg=AFQjCNF2_ZGdd_aKEdl-KOsq_r9UnUi66Q&bvm=bv.71778758,d.cWc\"相对=nofollow>在这里

我想通过我的身体物理的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] {

}

但我得到这个错误。

But I get this error.

致命错误:数组不能垂头丧气,以衍生阵列

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

 }

有没有故障,而是身体变成零。

There is no crash, but "body" becomes nil.

如果我不投所有,我没有得到一个崩溃。例如:

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的测试版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包含的对象,然后投失败,而机构将成为零,所以我不能循环遍历。如此看来,目前它仅仅是不可能通过的allContactedBodies数组投AnyObject到SKPhysicsBody,使其无法循环​​,除非有人可以提供一个解决办法。

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:错误仍然在X code 6测试版5.解决方法下面贴仍然作品 -
编辑4:错误仍然在X code 6测试6解决方法下面贴仍然作品 -
编辑5:失望。错误还在X code 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:我已经收到以下消息来自苹果:

工程提供了以下信息:

我们相信这个问题已经在最新版的X code 6.1测试版得到解决。

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

但它不是,错误仍然在X $ C $Ç6.1.1!解决方法仍然有效。

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

修改7: X $ C $ 6.3Ç,依然不动,解决办法仍然有效。

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

推荐答案

多的试验和错误之后,我发现了一个解决方法,以我的问题。事实证明,你并不需要在所有垂头丧气访问SKPhysicsBody的属性,当类型为AnyObject。

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
        }
    }

这篇关于斯威夫特:无法垂头丧气AnyObject到SKPhysicsBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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