Apple has the following method in the SKPhysicsBody class.
func allContactedBodies() -> [AnyObject]!
I noticed that it returns an AnyObject array. So I read about how to deal with AnyObject top-down casting. Here
I want to iterate over the allContactedBodies array of my physical body. The problem is that no matter what I try, I just can't get it to work.
I tried this first:
for body in self.physicsBody.allContactedBodies() as [SKPhysicsBody] { }
But I get this error.
fatal error: array cannot be converted to an array of derivatives
I also tried this:
for object in self.physicsBody.allContactedBodies() { let body = object as SKPhysicsBody }
But it also crashes with the following:

And similarly, I tried this:
for object in self.physicsBody.allContactedBodies() { let body = object as? SKPhysicsBody }
There is no collapse, but the "body" becomes zero.
And if I donโt give up at all, I will not fail. For instance:
for object in self.physicsBody.allContactedBodies() { }
But obviously, I need to include if I want to use the actual type.
So, as a test, I just tried this:
let object: AnyObject = SKPhysicsBody() let body = object as SKPhysicsBody
And this also leads to the same failure as in the picture.
But other types will not break. For example, this will not work.
let object: AnyObject = SKNode() let node = object as SKNode
So my question is: how can I properly scroll through the allContactedBodies array ?
Edit: I am running Xcode 6 beta 4 on an iOS 8 beta 4 device.
Edit 2: Additional Information
Ok, so I just did a few more tests. I tried this:
let bodies = self.physicsBody.allContactedBodies() as? [SKPhysicsBody]
If "allContactedBodies" is empty, then the transfer is successful. But if "allContactedBodies" contains objects, then it is reset, and the "bodies" become zero, so I can not miss it. It seems like it is currently impossible to add AnyObject to SKPhysicsBody, which makes it impossible to scroll through the allContactedBodies array unless someone can provide a workaround.
Edit 3: Error in Xcode 6 beta 5. Workaround posted below still works
Edit 4: Error in Xcode 6 beta 6. Workaround posted below still works
Edit 5: Disappointed. Error in Xcode 6 GM. The workaround posted below still works
EDIT 6: I received the following message from Apple:
Engineering provided the following information:
We believe this issue was addressed in the latest version of Xcode 6.1.
BUT THIS IS NOT, the error is still in Xcode 6.1.1 !!! The workaround still works.
Edit 7: Xcode 6.3, still not fixed, workaround still works.