// 将返回的json字符串转Dictory let json = """ { "name": "jack", "age": 20, "description": "A student." } """.data(using: .utf8)! if let jsonData = jsonString.data(using: .utf8) { do { let json = try JSONSerialization.jsonObject(with: jsonData, options: []) if let dictionary = json as? [String: Any], let key2 = dictionary["key2"] as? [String: Any], let innerKey2 = key2["key2"] as? [String: Any], let value = innerKey2["key1"] as? String { print(value) // 输出"value3" } } catch { print("解析JSON数据失败:(error)") } }
// 定义数据类型,遵守Codable协议 // 注意定义的类型与接口返回对应,否则JSONSerialization时崩溃 // Struct模型可以根据json中的数据关系对应嵌套 struct ContactSimpleModel: Codable { var relation: String var name: String struct ContactSimpleModel1: Codable { var relation: String var name: String struct ContactSimpleModel2: Codable { var relation: String var name: String } } }
文章来源地址https://www.toymoban.com/news/detail-433639.html
字典与struct互相转换
let decoder = JSONDecoder() let resList = try widgets.map { (item) -> AdJsonModel in let data = try JSONSerialization.data(withJSONObject: item, options: []) let res = try decoder.decode(AdJsonModel.self, from: data) return res } return resList
// 扩展 Encodable 协议 extension Encodable { var dictionary: [String: Any]? { if let data = try? JSONEncoder().encode(self) { if let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] { return dict } return nil } return nil } }
struct Person: Codable { var name: String var age: Int } var people = [Person(name: "John", age: 30), Person(name: "Mary", age: 25)] //转成data保存 let data = try! JSONEncoder().encode(people) UserDefaults.standard.set(data, forKey: "peopleData") //取 if let data = UserDefaults.standard.data(forKey: "peopleData") { let people = try! JSONDecoder().decode([Person].self, from: data) }
lazy var adListDirPath: String? = { let documentDic = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first let adUrl = documentDic?.appendingPathComponent(":ad") if let adUrlSub = adUrl { var isDic: ObjCBool = ObjCBool(false) // 判断adUrlSub.path路径是否存在,如果是文件路径则isDic=false, 如果是目录路径isDic=true if FileManager.default.fileExists(atPath: adUrlSub.path, isDirectory: &isDic) && isDic.boolValue { return adUrlSub.absoluteString } do { // 如果路径不存在,则创建本地路径,withIntermediateDirectories: true表示如果路径中间有未创建的,则把中间的目录也创建 try FileManager.default.createDirectory(at: adUrlSub, withIntermediateDirectories: true, attributes: nil) return adUrlSub.absoluteString } catch { Logger.error(":adDataManager", content: error.localizedDescription) return nil } } return nil }()
1.先删除历史文件 do { try FileManager.default.removeItem(at: filePathUrl) return true } catch { Logger.error(":adDataManager", content: error.localizedDescription) return false } 2.保存新文件 NSDictionary(object: adList, forKey: adListRootKey).write(to: filePathUrl, atomically: true) 3.读取本地文件 let dict = try NSDictionary(contentsOf: filePathUrl, error: ())
func unzipFile() { let zipPath = "/path/to/zip/file" let destinationPath = "/path/to/destination/folder" do { try ZipArchive.unzipFile(atPath: zipPath, toDestination: destinationPath) } catch { print("Error unzipping file: (error.localizedDescription)") } }
文章来源:https://www.toymoban.com/news/detail-433639.html
//0.自定义设置缓存大小 URLCache.shared = URLCache(memoryCapacity: 10 * 1024 * 1024, diskCapacity: 50 * 1024 * 1024, diskPath: nil) //1.缓存 SDWebImage批量下载图片 SDWebImagePrefetcher.shared().prefetchURLs方法下载的图片本地路径默认是Library/Caches/com.hackemist.SDWebImageCache.default目录下的缓存文件。 可以通过SDWebImageManager.shared().imageCache.diskCachePath方法获取具体路径。 //2.使用 在UIImageView加载图片时,使用AAImageView.sd_setImage(with: url, placeholderImage: nil)进行价值
//3.删除缓存 SDWebImageManager.shared.imageCache.removeImage(forKey: $0, cacheType: SDImageCacheType.all)
let list = dict[adListRootKey] as? Array<[String: Any]> if let listSub = list { let decoder = JSONDecoder() let resList = listSub.map { (item) -> AdJsonModel? in
到了这里,关于Swift下Data处理全流程:从网络下载,数模转换,本地缓存到页面使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!