我試圖從Objc轉換到Swift,並有更好的日子。如何使用SwiftyJSON從文件保存/檢索字典
我有一個類的字典:
collaborationDictionary:[String:Set<String>]
我想讀/寫此字典/從一個文件,只是不能完全似乎使其工作。我必須使用以下JSON結構來保存字典,並且必須使用SwiftyJSON。
{ "Collaborations" : {
"5604" : [
"whiteboard.png",
"VID_20161123_135117.3gp",
"Photo_0.jpeg"]
"5603" : [
"VID_20161123_135117.3gp"],
"5537" : [
"Screenshot_20151212-132454.png",
"VID_20161202_083205.3gp",
"VID_20161123_135117.3gp",
"Photo_0.jpeg",
"Screenshot_20151212-132428.png",
"Screenshot_20151212-132520.png",
"IMG_20161017_5.jpg",
"whiteboard.png"]}
}
我沒有找到/檢索文件或寫入文件的任何實際問題。我只是不能完全弄清楚如何手動加載SwiftyJSON。我需要在頂部有一個名爲「Collaborations」的JSON對象。它需要包含協作ID字典(5604,5603 ...)。每個協作包含一個字符串(文件名)數組。我包括我用來讀/寫文件的代碼,但我需要SwiftyJSON庫的幫助。
這是我使用存儲上述數據的構件數據成員:
這些是我需要完成以下功能:
private var collaborationDictionary:[String:Set<String>] = [:]
func getUploadedFileSet() {
collaborationDictionary = [:]
let documentsURL = URL(string: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let appURL = documentsURL?.appendingPathComponent(APP_DISTINGUISHED_NAME)
let jsonFileURL = appURL?.appendingPathComponent(UPLOADED_ITEMS_DB_JSON)
if FileManager.default.fileExists(atPath: (jsonFileURL?.absoluteString)!) {
do {
let data = try Data(contentsOf: jsonFileURL!, options: .alwaysMapped)
let json = JSON(data: data)
// ************************************************
// NEED HELP START
// NOW WHAT???? What is the SwiftyJSON code
?????????????????????????
// NEED HELP END
// ************************************************
} catch let error {
print(error.localizedDescription)
}
}
}
func saveUploadedFilesSet() {
let documentsURL = URL(string: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let appURL = documentsURL?.appendingPathComponent(APP_DISTINGUISHED_NAME)
let jsonFileURL = appURL?.appendingPathComponent(UPLOADED_ITEMS_DB_JSON)
do {
let dirExists = FileManager.default.fileExists(atPath: (appURL?.absoluteString)!)
if !dirExists {
try FileManager.default.createDirectory(atPath: (appURL?.absoluteString)!, withIntermediateDirectories: false, attributes: nil)
}
// ************************************************
// NEED HELP START
// NOW WHAT???? What is the SwiftyJSON code
?????????????????????????
// NEED HELP END
// ************************************************
// Write to file code - haven't written it yet but that should be easy
} catch let error as NSError {
print(error.localizedDescription);
}
}
任何方向將不勝感激。謝謝!
編輯
我能弄清楚如何從文件加載附帶的JSON結構。這裏是代碼:
func getUploadedFileSet() {
let documentsURL = URL(string: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let appURL = documentsURL?.appendingPathComponent(APP_DISTINGUISHED_NAME)
let jsonFileURL = appURL?.appendingPathComponent(UPLOADED_ITEMS_DB_JSON)
if FileManager.default.fileExists(atPath: (jsonFileURL?.absoluteString)!) {
do {
let data = try Data(contentsOf: jsonFileURL!, options: .alwaysMapped)
let json = JSON(data: data)
if json != nil {
for (key, subJson) in json[kCollaborations] {
let stringArray:[String] = subJson.arrayValue.map { $0.string! }
let stringSet = Set(stringArray)
collaborationDictionary.updateValue(stringSet, forKey: key)
}
} else {
print("Could not get json from file, make sure that file contains valid json.")
}
} catch let error {
print(error.localizedDescription)
}
}
我還沒有想出如何將collaborationDictionary對象保存到文件。我最大的問題是弄清楚如何放入「協作」鍵。有任何想法嗎?
你的具體問題是什麼? – Raphael
具體來說,讀取和寫入json對象的代碼是什麼。我將重點介紹代碼需要驗證/修復或創建的部分。 – JustLearningAgain
長話短說,你需要從磁盤加載文件?閱讀它作爲字符串?並使用SwiftyJSON序列化爲json? –