If I try to change the value of the speed parameter of a GKAgent2D (or its parent class GKAgent) in iOS9 I get this unrecognised selector error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GKAgent2D setSpeed:]: unrecognized selector sent to instance
However in iOS10 this error does not occur and the speed of the agent is changed correctly.
I can reproduce the error in a very simple example (single view iOS application, change the view controller code to the following, note that the error occurs with either GKAgent2D or GKAgent):
import UIKit
import GameplayKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var agent = GKAgent()
agent.speed = 10
print(agent.speed)
}
}
The above always crashes with this unrecognised selector error on the line that sets the agent's speed on the simulator running iOS9.3, but not iOS10.3 (all this under Xcode 8.3.2).
The speed property of GKAgent is documented as being read and write and being supported under iOS9 - see Apple's Documentation for GKAgent, speed Property.
I had a similar problem with GKAgentDelegate, which would crash on iOS9 only with an unrecognised selector to agentWillUpdate. I worked around this by putting a dummy method in my code:
func agentWillUpdate(_ agent: GKAgent) {
}
I have tried a similar solution to the new error, by overriding the speed property in my GKAgent2D sub class and providing an explicit setter and getter, and even backing the speed parameter by a private Float as suggested by Kdawg, but the same error still occurs when the super's speed parameter is set:
class Agent:GKAgent2D {
override var speed:Float {
get {return localSpeed}
set {localSpeed = newValue}
}
private var localSpeed:Float {
get {return super.speed}
set {super.speed = newValue}
}
}
Any thoughts?
Specifically: are there any known issues with GKAgent in iOS9 regarding selectors?
Alternatively, any thoughts on an alternative work around?
I would like to support iOS9 if I can. It looks to me like a bug in GameplayKit - but I expect Apple's response to a report would be that it is fixed in iOS10.