关于ios:模拟器网络-json-转-plist-文件便捷代码

33次阅读

共计 607 个字符,预计需要花费 2 分钟才能阅读完成。

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

正文完
 0