模拟器,网络 json 转 plist 文件,便捷代码

模拟器,网络 json 转 plist 文件,

Net.request(.get(key)) { (result) in
            do{
                let data = try result.get().data
                do{
                    let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
                    if let dict = json as? [String: Any], let src = dict["data"] as? NSDictionary{
 
                        // 写入数据
                        src.write(toFile: "/Users/Pictures/one.plist", atomically: true)
                    }
                }
                catch let error{
                    print(error)
                }
 
 
            }
            catch{
                print(error)
            }
        }

便捷的写法,当然是 NSDictionary 写入路径字符串



配套的解析代码:

if let src = Bundle.main.url(forResource: "one", withExtension: "plist"){
            do {
                let data = try Data(contentsOf: src)
                let decoder = PropertyListDecoder()
                let resp = try decoder.decode(HanCatalog.self, from: data)
                // ...
            } catch  {
                print(error)
            }
            
        }

你可能感兴趣的:(ios)