Reputation: 299
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.
Upvotes: 3
Views: 4375
Reputation: 2738
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;
}
Upvotes: 1
Reputation: 72790
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 {
Upvotes: 4