Swift programming question from my son

Owen Hartnett owen at clipboardinc.com
Sat Oct 10 11:10:14 EDT 2015


I have some thoughts about why this is so, but I’m not completely sure, so I present this to the experts on the list.

-Owen


> Another programming question….
> 
> 
> This is Swift again, so I don’t know if you’d know the answer, but I was trying to solve one of the sprite kit tutorial challenges, and thought I did it, but it didn’t work. When I looked at the solution, it seemed very similar to what I had done, but I can’t see the difference as to why their solution worked, and mine didn’t.
> 
> 
> A sprite has a dictionary property that you can put things into for storage. The challenge was to count the number of times this sprite made contact with the edge of the scene, and put this in the dictionary property. If it’s the first time, create a dictionary with the key “bounceCount” and a value of 1. If the property already contains a dictionary, increment the value by 1. “label” is the SKLabelNode sprite that they created. The line they give you to create the dictionary is
> 
> 
> "Please note that userData is of the precise type NSMutableDictionary. That means,
> when you create a new dictionary to store in userData, you’ll have to do it like so
> to match the required data type:
> 
> label.userData = NSMutableDictionary(object: 1 as Int, forKey: "bounceCount”)
> 
> "
> 
> the incremental step is the part that didn’t work for me… "Check to see if the label’s userData dictionary is nil. If it is, create a new mutable
> dictionary with a single entry with the key bounceCount and the value of 1 as an
> Int. If userData isn’t nil, look up the bounceCount entry and increment the
> number.”
> 
> 
> the solution is (showing the increment step ONLY)
> 
> if var userData = label.userData {
>                    userData["bounceCount"] = (userData["bounceCount"] as! Int) + 1
>                } 
> 
> my version does not work. here it is:
> 
> if label.userData != nil {
>                label.userData["bounceCount"] = (label.userData["bounceCount"] as! Int) + 1
>                }
> 
> the error message is “! ‘((Int), IntegerLiteralCovertible)’ is not convertible to ‘StringLiteralConvertible’”
> 
> any idea? I’m wondering if it’s because they put it into a variable before working with it in their solution, which makes it mutable automatically, but if the property is a variable anyway, I would think it should still be mutable..

> Here’s the whole function. didBeginContact is one of the delegate/protocol functions when you elect to receive messages from the physics simulation. The commented out code at the bottom is my code that is spitting out the error. Their solution is right above it.
> 
> 
> func didBeginContact(contact: SKPhysicsContact) {
> 
>        let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
>        if collision == PhysicsCategory.Cat | PhysicsCategory.Bed {
>            win()
>        } else if collision ==
>            PhysicsCategory.Cat | PhysicsCategory.Edge {
>                lose()
>        }
>        if collision ==
>            PhysicsCategory.Edge | PhysicsCategory.Label {
>                let label: SKLabelNode
>                let edge: SKNode
>                if (contact.bodyA.categoryBitMask == PhysicsCategory.Label) {
>                    label = contact.bodyA.node as! SKLabelNode
>                    edge = contact.bodyB.node!
>                } else {
>                    label =  contact.bodyB.node as! SKLabelNode
>                    edge = contact.bodyA.node!
>                }
>                if var userData = label.userData {
>                    userData["bounceCount"] = (userData["bounceCount"] as! Int) + 1
>                } else {
> 
>                    label.userData = NSMutableDictionary(object: 1 as Int, forKey:
>                        "bounceCount")
>                }
>       //         if label.userData != nil {
>       //         label.userData["bounceCount"] = (label.userData["bounceCount"] as! Int) + 1
>       //         } else { label.userData = NSMutableDictionary(object: 1 as Int, forKey:
>      //                  "bounceCount")
> 
> 
>        }
>    }



More information about the MacTechGroup-discuss mailing list