gpt4 book ai didi

ios - 如何使用 SwiftyJSON 从文件中保存/检索字典

转载 作者:IT王子 更新时间:2023-10-29 05:36:50 25 4
gpt4 key购买 nike

我正在努力实现从 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 对象保存到文件中。我最大的问题是弄清楚如何输入“协作”键。有什么想法吗?

最佳答案

我终于让它工作了。最大的问题是我无法将 collaborationDictionary 转换为 JSON。我最终不得不将它转换为数组字典与集合字典。以下是 2 种方法:

     // **************************************************************************
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)
}
}
}

// **************************************************************************
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)
let adjustedJSONFileURL = URL(fileURLWithPath:(jsonFileURL?.absoluteString)!)

do {
let dirExists = FileManager.default.fileExists(atPath: (appURL?.absoluteString)!)
if !dirExists {
try FileManager.default.createDirectory(atPath: (appURL?.absoluteString)!, withIntermediateDirectories: false, attributes: nil)
}

// Convert set elements to arrays
var convertedCollaborationDictionary: [String:[String]] = [:]
for (sessionID, fileNameSet) in collaborationDictionary {
let array = Array(fileNameSet)
convertedCollaborationDictionary.updateValue(array, forKey: sessionID)
}
let json: JSON = JSON(convertedCollaborationDictionary)
let fullJSON: JSON = [kCollaborations:json.object]
let data = try fullJSON.rawData()
try data.write(to: adjustedJSONFileURL, options: .atomic)

} catch let error as NSError {
print(error.localizedDescription);
}
}

关于ios - 如何使用 SwiftyJSON 从文件中保存/检索字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41144974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com