Thanks for your response. I tried the breakpoint and didn't get any more info. Yes, "category" was the key I was using. I made a test project following these steps:
Step 1. Make a new SpriteKit game template project from Xcode 9.2.
Step 2. Drag a colored sprite into the scene
Step 3. Click on the new colored sprite and hit + in the attributes inspector to add some default user data to the sprites userData?.
Step 4. Add the following function to the GameScene and call it from touchesBegan().
func setupSpriteDictionaries(){
self.enumerateChildNodes(withName: "*") {
node, stop in
if (node.userData == nil){
node.userData = NSMutableDictionary()
}
if let sprite = node as? SKSpriteNode {
var dictionary:NSMutableDictionary! = sprite.userData?.mutableCopy() as! NSMutableDictionary
print ("Sprite n \(String(describing: sprite))")
var tempcategory = 42
print("\(sprite.userData!) <---shows data input from .sks file.")
/*sprite.userData?["category"] = tempcategory*/
/*sprite.userData?.setValue(tempcategory, forKey: "category")*/
dictionary["category"] = tempcategory
sprite.userData = dictionary
print("\(sprite.userData!) <---shows combined data using new dictionary")
print("Now set sprite's userData with the new dictionary using the same code that crashed")
sprite.userData?["category"] = tempcategory+=1
sprite.userData?.setValue(tempcategory, forKey: "category")
print("\(sprite.userData!) <---again, combined data from new dictionary")
}
}
}
Step 5. Run to see output. Output was originally posted here but caused this post to be stuck in moderation.
Step 6. Uncomment line 14 and run. Output:
:-[__NSSingleEntryDictionaryI _swift_setObject:forKeyedSubscript:]: unrecognized selector sent to instance 0x60c00003f3a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleEntryDictionaryI _swift_setObject:forKeyedSubscript:]: unrecognized selector sent to instance 0x60c00003f3a0'
Step 7. Recomment line 14, uncomment line 15 and run. Output:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSSingleEntryDictionaryI 0x608000224da0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key category.'
It still looks to me like an immutable dictionary being stored by XCode. I never had this happen previously and I was adding data to those dictionaries quite a bit.