3

After updating to xCode 6.3 / Swift 1.2 I have run into an error I can't resolve: "Cast from '[SKNode]' to unrelated type 'String' always fails".

Below is my GameScene code which begins by parsing a p-list.

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate, NSXMLParserDelegate {

    var currentTMXFile:String?

    override func didMoveToView(view: SKView) {

        /* Parse p-list */

        let path = NSBundle.mainBundle().pathForResource("GameData", ofType: "plist")
        let dict = NSDictionary(contentsOfFile: path!)!
        let playerDict:AnyObject = dict.objectForKey("PlayerSettings")!
        let gameDict:AnyObject = dict.objectForKey("GameSettings")!
        let levelArray:AnyObject = dict.objectForKey("LevelSettings")!

        if let levelNSArray:NSArray = levelArray as? NSArray {

            var levelDict:AnyObject = levelNSArray[level]

            numberOfLevels = levelNSArray.count

            if let tmxFile = levelDict["TMXFile"] as? String {

                currentTMXFile = tmxFile

            }

        }

The error is being thrown up at...

if let tmxFile = levelDict["TMXFile"] as? String {...

and is returning a nil value, even though there is a string value in the p-list and prior to updating, it was working fine. I have tried casting as AnyObject first and this throws up more errors.

2 Answers2

4

Here:

var levelDict:AnyObject = levelNSArray[level]

you are declaring levelDict as AnyObject, but here:

if let tmxFile = levelDict["TMXFile"] as? String {

you are using it as if it were a dictionary - I wonder how it's possible that it worked in a previous version of Xcode...

However, to fix it, make that variable an explicit dictionary, either a cocoa one:

var levelDict = levelNSArray[level] as? NSDictionary

if let tmxFile = levelDict?.objectForKey("TMXFile") as? String {

or a swift one:

var levelDict = levelNSArray[level] as? [String : AnyObject]

if let tmxFile = levelDict?["TMXFile"] as? String {
Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Thanks, that fixed it. The items in the array are dictionaries from the p-list. I think that's why AnyObjects are being used as Dictionaries in this case. So the swift answer you give here doesn't work, but a combination of both does work. – James Stephen Brown Apr 14 '15 at 17:32
1

If you happen to be doing this with the dynamicType of an object in a case statement, here's how:

     switch (self.dynamicType) {
        case is MyClass.Type:
            print("MyClass")
            break;
     }
snibbe
  • 2,715
  • 1
  • 27
  • 34