Error after upgrading XCode, ios 8.1, spritekit and swift, constructing object 'NSData (contentsOfFile: options: error :)'

I have a working game and I ran, compiled and uploaded it to iTunes connect. But after updating XCode and trying to compile my game with target ios 8.1 (not 8.0). I got this error.

extension SKNode {
    class func unarchiveFromFile(file : NSString) -> SKNode? {

        let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks")

// ERROR: 
// 'dataWithContentsOfFile(_:options:error:)' is unavailable: use object construction 'NSData(contentsOfFile:options:error:)'

        var sceneData = NSData.dataWithContentsOfFile(path!, options: .DataReadingMappedIfSafe, error: nil)


        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
        archiver.finishDecoding()
        return scene
    }
}

      

I haven't touched on the unarchiveFromFile method here and from google search. I couldn't find anyone with the same problem. Really lost here.

EDIT: updated the code to this (after comment)

extension SKNode {
    class func unarchiveFromFile(file : NSString) -> SKNode? {

        let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks")

        var sceneData = NSData.dataWithContentsOfFile(path!, options: .DataReadingMappedIfSafe, error: nil)
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
        archiver.finishDecoding()
        return scene
    }
}

      

Now it compiles and runs, but then it crashes imitatively!

I only get this:

dyld`dyld_fatal_error:
0x1fe1e08c:  trap   
0x1fe1e090:  nop     

      

+3


source to share


1 answer


you may try:



extension SKNode {
    class func unarchiveFromFile(file : NSString) -> SKNode? {

    if let path = NSBundle.mainBundle().pathForResource(file as String, ofType: "sks") {

        var sceneData = NSData()
        do {
            try sceneData = NSData(contentsOfFile: path, options:NSDataReadingOptions.DataReadingMappedIfSafe)

        } catch {
            abort()
        }

        let archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}

      

+2


source







All Articles