didBeginContact passed PKPhyicsObject
I get the same error with this simple code:
extension SKPhysicsContact {
func bodiesMatchingCategory(mask: UInt32) -> [SKPhysicsBody] {
let bodies = [bodyA, bodyB]
return bodies.filter { ($0.categoryBitMask & mask) != 0 }
}
}
let contact = SKPhysicsContact()
let body = contact.bodiesMatchingCategory(0)
The problem is, the type of contact
is PKPhysicsContact
(as you've noticed), even when you explicitly tell it to be an SKPhysicsContact
, and the extension is on SKPhysicsContact
. You'd have to be able to make an extension to PKPhysicsContact
for this to work. From this logic, we can say that no instance methods will work in SKPhysicsContact
extensions at the moment. I'd say it's a bug with SpriteKit, and you should file a radar. Class methods still work since you call them on the class itself.
In the meantime, you should be able to move that method into your scene or another object and call it there successfully.
For the record, this is not a Swift-specific problem. If you make the same method in an Objective-C category on SKPhysicsContact
you'll get the same crash.
extension for SKPhysicsContact crashing
This apparently is a bug, as answered here:
https://stackoverflow.com/a/33423409/6593818
The problem is, the type of contact is PKPhysicsContact (as you've noticed), even when you explicitly tell it to be an SKPhysicsContact, and the extension is on SKPhysicsContact. You'd have to be able to make an extension to PKPhysicsContact for this to work. From this logic, we can say that no instance methods will work in SKPhysicsContact extensions at the moment. I'd say it's a bug with SpriteKit, and you should file a radar. Class methods still work since you call them on the class itself.
In the meantime, you should be able to move that method into your scene or another object and call it there successfully.
For the record, this is not a Swift-specific problem. If you make the same method in an Objective-C category on SKPhysicsContact you'll get the same crash.
You can submit a bug report to apple:
https://developer.apple.com/bug-reporting/
And report it to the community:
https://openradar.appspot.com/search?query=spritekit
However, what you really want to do with your code is to add the category masks together. And then check for the sum (2 + 4 and 4 + 2 always equals 6, regardless of bodyA and bodyB order).
This is how you get unique contacts, if you set up your masks correctly in powers of two (2, 4, 8, 16, etc)
Related Topics
How to Detect If a Nsattributedstring Contains a Nstextattachment and Remove It
Get Center Coordinates from Mapkit and Display in Uilabel
Libmobilegestalt Mobilegestalt.C:890: Mgisdeviceoneoftype Is Not Supported on This Platform
How to Refresh a Timer in a Today Widget
My App Is Rejected with Exception Exc_Breakpoint (Sigtrap)
Uidocumentinteractioncontroller() Swift
How to Add File Picker to the App on iOS 14+ and Lower
How Would I Store a Video into Firebase Storage from Swift
How to Add a Ibaction to a Button Programmatically in Swift 4
How to Get All the Sundays in Array of Date iOS
Manage a Uipickerview from an External Class - Using Swift
Drawing an Infinite Grid in iOS
Swift - Parse a String Which Contains a Url