1

模拟器,网络 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)
            }
            
        }

black_pearl
79 声望5 粉丝

火星时代, 未来是太空