2017-04-05 66 views
2

我在iPad(v10.2.1)上使用Xcode(version 8.2.1)製作遊戲,當我試圖在沙盒中的文檔文件夾中保存一些數據時,我發現所有我嘗試過的方法失敗:createDirectorycreateFile(using file manager),以及writeToUrl(使用NSData)。我在2臺iPad和1部iPhone上運行了該程序,但沒有成功。無法在iOS沙盒中的文檔文件夾中創建文件或目錄

我能夠從我創建的plist中讀取,並使用函數convertDataFrom()將其轉換爲字典。

我是新來的數據持久性或文件操作,我的遊戲卡在這裏。將不勝感激任何幫助!

func loadData()->NSDictionary{ 
    let fm = FileManager() 

    let sourceUrl = Bundle.main.url(forResource: "Data", withExtension: "plist")! 
    var appSupportDir = fm.urls(for: .documentDirectory, in: .userDomainMask).first! as? NSURL 
    appSupportDir = appSupportDir!.appendingPathComponent("UserData", isDirectory: true) as NSURL? 
    let urlForSave = (appSupportDir!.appendingPathComponent("Data.plist"))! 


    //load data, or if file doesn't exist yet, create one 
    var isDirectory: ObjCBool = false 

    if fm.fileExists(atPath: urlForSave.absoluteString, isDirectory: &isDirectory) { 
     let data = convertDataFrom(url: urlForSave) 
     print("data file exists") 
     print(isDirectory) 

     return data as NSDictionary 

    } else { 
     do { try fm.createDirectory(atPath: appSupportDir!.absoluteString!, withIntermediateDirectories: true, attributes: nil) }catch let Error { 
      print("create directory failed") 
      print(Error) 
     } 

     let originalData = NSData.init(contentsOf: sourceUrl) 
     print("\(urlForSave)") 
     do { try originalData?.write(toFile: urlForSave.absoluteString, options: .atomic) } catch let Error { 
      print(Error) 
     } 
     if (!fm.createFile(atPath: urlForSave.absoluteString, contents: originalData as Data?, attributes: nil)) { 
      fatalError("file creation failed") 
     } 

     let data = convertDataFrom(url: urlForSave) 
     print("\(data)") 
     print(isDirectory) 

     return data as NSDictionary 
    } 

} 

func convertDataFrom(url: URL)->Dictionary<String, Any> { 
    let dictionary = NSDictionary.init(contentsOf: url) as! Dictionary<String, Any> 
    return dictionary 
} 

Error messages for "createDirectory": Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file 「UserData」 in the folder 「Documents」." UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/F7F41E0D-D3F6-489B-A59E-B7AC401EC402/Documents/UserData/, NSUnderlyingError=0x174053f50 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} file:///var/mobile/Containers/Data/Application/F7F41E0D-D3F6-489B-A59E-B7AC401EC402/Documents/UserData/Data.plist

and for "createFile" (fatalError if createFile fails) Error Domain=NSCocoaErrorDomain Code=4 "The file 「Data.plist」 doesn’t exist." UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/F7F41E0D-D3F6-489B-A59E-B7AC401EC402/Documents/UserData/Data.plist, NSUnderlyingError=0x174055bd0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} fatal error: file creation failed: file /Users/chanwu/Desktop/Math_DownStairs/Math_DownStairs/

回答

1

你的代碼檢查,看是否urlForSave(最後文件)存在,如果沒有,你再嘗試創建的UserData目錄。

如果最終文件不存在,但是UserData目錄確實存在,我會期待您描述的確切結果。

您應該更改測試以查看UserData目錄是否存在,而不是測試最終文件。這樣,如果目錄不存在,您將創建該目錄,然後致電write(toFile:),如果該目錄不存在,則該目錄將創建該文件。

+0

謝謝我按照你的建議做了。但看起來這不是根本問題。我現在使用nsuserdefaults,因爲我只需要保存一些數據。 – Chandler

相關問題