【发布时间】:2017-04-29 22:09:35
【问题描述】:
我正在尝试从 Objc 转换为 swift 并且过得更好。
我有一堂带字典的课:
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_132105.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 对象保存到文件中。我最大的问题是弄清楚如何输入“协作”键。有什么想法吗?
【问题讨论】:
-
您的具体问题是什么?
-
具体来说,读写json对象的代码是什么。我将突出显示需要验证/修复或创建代码的部分。
-
长话短说,您需要从磁盘加载文件吗?把它读成字符串?并使用 SwiftyJSON 序列化为 json?
-
我明白这一点。我不知道该怎么做是专门使用“SwiftyJSON”。我正在更新上面的代码,以便您可以编写 ACTUAL swiftyJSON 来读取和写入 JSON 字符串。
-
不是答案,但我强烈建议您在学习一门新语言时不要使用第三方库。
标签: ios swift swifty-json