// 將返回的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 } } }
字典與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)") } }
//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)進行價值
let list = dict[adListRootKey] as? Array<[String: Any]> if let listSub = list { let decoder = JSONDecoder() let resList = listSub.map { (item) -> AdJsonModel? in