From owen at clipboardinc.com Sat Oct 10 11:10:14 2015 From: owen at clipboardinc.com (Owen Hartnett) Date: Sat, 10 Oct 2015 11:10:14 -0400 Subject: Swift programming question from my son Message-ID: <690F0C32-E8A3-44CD-8715-F5C70F9276D8@clipboardinc.com> 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") > > > } > } From john at johncshockey.com Sat Oct 10 12:09:49 2015 From: john at johncshockey.com (John Shockey) Date: Sat, 10 Oct 2015 09:09:49 -0700 Subject: Swift programming question from my son Message-ID: <1444493389.76961.406652681.659652AE@webmail.messagingengine.com> Owen, I haven't done a lot with Swift, since my current project predates the language. I can't make a lot of sense of the error message. It's a pretty bad one! But I can see where the difference between the two versions would matter. if label.userData != nil { // ... } tells you that userData won't be nil within the braces. But if var userData = label.userData { // ... } tells both you AND the compiler that it isn't nil So in their version userData is typed as a dictionary, but in your son's version label.userData is still typed as an optional dictionary. Seems pretty academic, but in fact another thread could set label.userData to nil, but the "if var" would be protected. But the real point is about the types. Possibly (but I have not done a lot with Swift, remember, so I'm a little unsure of the syntax) adding a couple of "!" operators would make his version work. Maybe: label.userData!["bounceCount"] = (label.userData!["bounceCount"] as! Int) + 1 but don't quote me on that without checking it! My feeling is that comparing something to nil in Swift may be legal, and sometimes work, but it kind of goes against the grain of the language. John From scaine at mac.com Sat Oct 10 12:38:02 2015 From: scaine at mac.com (Steve Caine) Date: Sat, 10 Oct 2015 12:38:02 -0400 Subject: Swift programming question from my son In-Reply-To: <1444493389.76961.406652681.659652AE@webmail.messagingengine.com> References: <1444493389.76961.406652681.659652AE@webmail.messagingengine.com> Message-ID: <3465A30D-B128-4C1A-8256-317FCCB7C1C6@mac.com> C++ was notorious for obscure compiler errors; 10-15 lines of error message that had to be boiled down as text (using find/replace in a text editor) to reveal you were trying to assign to a const object. Perhaps the compilers are better these days. ? Steve Caine http://www.linkedin.com/in/stevecaine > On Oct 10, 2015, at 12:09 PM, John Shockey wrote: > > Owen, > > I haven't done a lot with Swift, since my current project predates the > language. I can't make a lot of sense of the error message. It's a > pretty bad one! > > But I can see where the difference between the two versions would > matter. > > if label.userData != nil > { > // ... > } > > tells you that userData won't be nil within the braces. But > > if var userData = label.userData > { > // ... > } > > tells both you AND the compiler that it isn't nil > > So in their version userData is typed as a dictionary, but in your son's > version label.userData is still typed as an optional dictionary. > > Seems pretty academic, but in fact another thread could set > label.userData to nil, but the "if var" would be protected. But the real > point is about the types. > > Possibly (but I have not done a lot with Swift, remember, so I'm a > little unsure of the syntax) adding a couple of "!" operators would make > his version work. Maybe: > > label.userData!["bounceCount"] = (label.userData!["bounceCount"] as! > Int) + 1 > > but don't quote me on that without checking it! > > My feeling is that comparing something to nil in Swift may be legal, and > sometimes work, but it kind of goes against the grain of the language. > > John > > _______________________________________________ > MacTechGroup-discuss mailing list > MacTechGroup-discuss at lists.mactechgroup.org > http://lists.mactechgroup.org/mailman/listinfo/mactechgroup-discuss From steve.sisak at ioxperts.com Sat Oct 10 13:59:31 2015 From: steve.sisak at ioxperts.com (Steve Sisak) Date: Sat, 10 Oct 2015 13:59:31 -0400 Subject: Swift programming question from my son In-Reply-To: <3465A30D-B128-4C1A-8256-317FCCB7C1C6@mac.com> References: <1444493389.76961.406652681.659652AE@webmail.messagingengine.com> <3465A30D-B128-4C1A-8256-317FCCB7C1C6@mac.com> Message-ID: Not as good as MPW C ;-) http://www.ralentz.com/old/mac/humor/mpw-c-errors.html > On Oct 10, 2015, at 12:38 PM, Steve Caine wrote: > > C++ was notorious for obscure compiler errors; 10-15 lines of error message that had to be boiled down as text (using find/replace in a text editor) to reveal you were trying to assign to a const object. Perhaps the compilers are better these days. > > ? > Steve Caine > http://www.linkedin.com/in/stevecaine > >> On Oct 10, 2015, at 12:09 PM, John Shockey wrote: >> >> Owen, >> >> I haven't done a lot with Swift, since my current project predates the >> language. I can't make a lot of sense of the error message. It's a >> pretty bad one! >> >> But I can see where the difference between the two versions would >> matter. >> >> if label.userData != nil >> { >> // ... >> } >> >> tells you that userData won't be nil within the braces. But >> >> if var userData = label.userData >> { >> // ... >> } >> >> tells both you AND the compiler that it isn't nil >> >> So in their version userData is typed as a dictionary, but in your son's >> version label.userData is still typed as an optional dictionary. >> >> Seems pretty academic, but in fact another thread could set >> label.userData to nil, but the "if var" would be protected. But the real >> point is about the types. >> >> Possibly (but I have not done a lot with Swift, remember, so I'm a >> little unsure of the syntax) adding a couple of "!" operators would make >> his version work. Maybe: >> >> label.userData!["bounceCount"] = (label.userData!["bounceCount"] as! >> Int) + 1 >> >> but don't quote me on that without checking it! >> >> My feeling is that comparing something to nil in Swift may be legal, and >> sometimes work, but it kind of goes against the grain of the language. >> >> John >> >> _______________________________________________ >> MacTechGroup-discuss mailing list >> MacTechGroup-discuss at lists.mactechgroup.org >> http://lists.mactechgroup.org/mailman/listinfo/mactechgroup-discuss > > _______________________________________________ > MacTechGroup-discuss mailing list > MacTechGroup-discuss at lists.mactechgroup.org > http://lists.mactechgroup.org/mailman/listinfo/mactechgroup-discuss