2014-11-01 45 views
3

我有一個工作的遊戲,我運行,編譯並上傳到iTunes連接。但在更新XCode並試圖用目標ios 8.1(不是8.0)編譯我的遊戲之後。我有這個錯誤。XCode升級,ios 8.1,spritekit和swift,對象構建'NSData(contentsOfFile:options: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 
    } 
} 

我還沒有觸及unarchiveFromFile方法在這裏,並從搜索谷歌我找不到任何人有同樣的問題。真的迷失在這裏。

編輯: 更新代碼到這個(評論後)

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 
    } 
} 

現在它編譯和運行,但隨後崩潰假定製作!

我只得到這樣的:

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

其實編譯器會告訴你用什麼:'NSData的(contentsOfFile:路徑!選擇:.DataReadingMappedIfSafe,錯誤:無)' – 2014-11-01 20:44:41

+0

謝謝,補充說它編譯。但這只是導致另一次崩潰,這次沒有錯誤信息.. – ganjan 2014-11-02 13:05:59

+0

可能相關:https://stackoverflow.com/questions/26836102/crash-on-instantiating-sklabelnode-swift-ios-8-1 – 2014-11-10 04:27:05

回答

2

你可以試試:

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 
    } 
} 
相關問題