The O.G. Virtual learning! Buy one, get 2 more Lifetime Memberships! - Click Here!

SKSpriteNode userdata set value crashing app

Home 4 Forums Swift and Sprite Kit SKSpriteNode userdata set value crashing app

This topic contains 1 reply, has 1 voice, and was last updated by  Unfrozen Young 2 years, 3 months ago.

  • February 14, 2019 at 1:55 pm #203534

    In the scene editor I’ve set one of my SKSpriteNodes to have userdata. I can read the data just fine but when I go to set the value the app crashes with the following error:
    Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<__NSDictionaryI 0x2812e5040> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key collected.’

    I set a break point and from the debugger I can look at my user data.
    print item
    (NSMutableDictionary) $R0 = 0x00000002812e5040 3 key/value pairs {
    [0] = {
    key = 0xe310c38480df548d “type”
    value = 0xf310c382d7d8c3da Int64(1)
    [1] = {
    key = 0xe310c5d5f12ef4fc “score”
    value = 0xf310c382d7d8c58a Int64(100)
    [2] = {
    key = 0xe3f0018ad45cc360 “collected”
    value = 0x000000024f8a1a28 NO

    I am currently using this code:
    let scoreAmount = scoreItem.userData![“score”] as! Int
    player1_score = player1_score + scoreAmount
    if let _ = scoreItem.userData
    let item = scoreItem.userData!
    item.setValue(true, forKey: “collected”)

  • March 5, 2019 at 12:30 am #203890

    Ended up just setting the values in code. I first created the mySprite.userData = NSMutableDictionary() then set the values with mySprite.userData.setValue(false, forKey: “collected”). No more errors.

You must be logged in to reply to this topic.

Know what an affiliate program is? You make money just by sharing links to our site! Win. Win.

Earn when you refer any buyer here! 30 day tracking. Commissions are 33%-50% and recur on subscription products!

Uh oh, missing your affiliate ID. Create one real quick