2017-01-01 114 views
0

我正嘗試使用JSON庫保存數據本地化。這是我目前的代碼:如何使用SwiftyJSON保存JSON數據

var jsonFile = "json.json" 

class ViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     let jsonFile = "file.json" 
     let jsonArray:JSON = [ 
      "array": [12.34, 56.78], 
      "users": [ 
       [ 
        "id": 987654, 
        "info": [ 
         "name": "jack", 
         "email": "[email protected]" 
        ], 
        "feeds": [98833, 23443, 213239, 23232] 
       ], 
       [ 
        "id": 654321, 
        "info": [ 
         "name": "jeffgukang", 
         "email": "[email protected]" 
        ], 
        "feeds": [12345, 56789, 12423, 12412] 
       ] 
      ] 
     ] 



     if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first 
     { 
      let path = dir.appendingPathComponent(jsonFile) 

      if(!FileManager.default.fileExists(atPath: path.path)) 
      { 
       FileManager.default.createFile(atPath: path.path, contents: Data(), attributes: nil) 
       print("Created file") 

       do { 
        try write(json: jsonArray, to: path) 
       } catch let error{ 
        print("Error: \(error.localizedDescription)") 
       } 

       try! write(json: jsonArray, to: path) 
      } else { 
       print("File existed") 
       do { 
        let myJSON = try readJSON(myURL: path) 
        if let _ = myJSON 
        { 
         print("Got data") 
        } else { 
         print("No Data") 
        } 
       } catch let errorOne { 
        print("Error2: \(errorOne.localizedDescription)") 
       } 
      } 
     } 
    } 


} 
let url = Bundle.main.url(forResource: "json", withExtension: "json") 


func readJSON(myURL:URL) throws -> JSON? { 

    if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first 
    { 
     let path = dir.appendingPathComponent(jsonFile) 

     if(!FileManager.default.fileExists(atPath: path.path)) 
     { 
      let data = try Data(contentsOf: path, options: .alwaysMapped) 
      return JSON(data: data) 
     } 
    } 
    return nil 
} 

func write(json: JSON, to url: URL) throws { 
    let data = try json.rawData() 
    if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first 
    { 
     let path = dir.appendingPathComponent(jsonFile) 

     if(!FileManager.default.fileExists(atPath: path.path)) 
     { 
      try data.write(to: path) 
     } 
    } 
} 

當您第一次運行該程序時,它會創建該文件並'寫入jsonArray'。當你再次運行它時,它會'加載'JSON數據。

問題是,readJSON(myURL: path)返回的值等於nil

如何在ios 10.2上使用SwiftyJSON讀取和寫入JSON數據?

更新

我已執行我在iPhone 6s模擬器應用程序,並檢查保存的文件json.json。這裏是保存的文件:

{"array":[12.34,56.78],"users":[{"id":987654,"info":{"name":"jack","email":"[email protected]"},"feeds":[98833,23443,213239,23232]},{"id":654321,"info":{"name":"jeffgukang","email":"[email protected]"},"feeds":[12345,56789,12423,12412]}]} 
+0

爲什麼不使用'try-catch'來處理錯誤?除此之外,即使文檔目錄中的文件存在,也始終無法創建JSON。 – vadian

+0

@vadian這只是玩圖書館的代碼。此代碼不會用於項目中。我只想知道如何保存和加載JSON數據。另外,我的程序不會崩潰。它只是返回零。 – iProgram

+0

'try-catch'不會**捕獲異常/崩潰。它報告錯誤。強烈建議在所有情況下使用它都可能會發生錯誤。調試你的代碼。設置斷點。使用調試器。 – vadian

回答

0

我剛剛解決了問題所在。

在readJSON()函數:

func readJSON(myURL:URL) throws -> JSON? { 

    if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first 
    { 
     let path = dir.appendingPathComponent(jsonFile) 

     if(!FileManager.default.fileExists(atPath: path.path)) 
     { 
      let data = try Data(contentsOf: path, options: .alwaysMapped) 
      return JSON(data: data) 
     } 
    } 
    return nil 
} 

if(!FileManager.default.fileExists(atPath: path.path))應該已經if(FileManager.default.fileExists(atPath: path.path))

之前,我只是在文件不存在時才加載數據。