Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error after XCode upgrade, ios 8.1, spritekit and swift, object construction '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 have not touched the unarchiveFromFile method here and from searching google I could not find anyone with the same problem. Really lost here.

EDIT: updated the code to this (after the 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 run, but then crash imitatively!

I only get this:

dyld`dyld_fatal_error:
0x1fe1e08c:  trap   
0x1fe1e090:  nop     
like image 863
ganjan Avatar asked Dec 08 '25 21:12

ganjan


1 Answers

you can 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
    }
}
like image 179
Jeferson Barros Avatar answered Dec 11 '25 10:12

Jeferson Barros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!