您好,登錄后才能下訂單哦!
這篇文章主要介紹Swift model解析的方法有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
1. 常規解析方法
//懶加載聲明一個LJNewsModel為數據的數組 lazy var ljArray : [LJNewsModel] = [LJNewsModel]()
//MARK:-- 數據獲取和解析 extension NewsViewController{ func requestNetData(){ /* 打印json數據 */ LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in 方法一:解析數據 let str = String(data:data!, encoding: String.Encoding.utf8)! print(str) let ljTempArray : NSArray = self.getDictionaryFromJSONString(jsonString: str).object(forKey: "T1348647909107") as! NSArray for m in 0 ..< ljTempArray.count { let dict:NSDictionary = ljTempArray[m] as! NSDictionary let model = LJNewsModel() model.imageUrl = dict.object(forKey: "imgsrc") as! String model.contentStr = dict.object(forKey: "title") as! String let count :Int = (dict.object(forKey: "replyCount") != nil) ? dict.object(forKey: "replyCount") as! Int : 0 model.replyCount = "\(count)" self.ljArray.add(model) } self.ljTablewView.reloadData() } } func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{ let jsonData:Data = jsonString.data(using: .utf8)! let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) if dict != nil { return dict as! NSDictionary } return NSDictionary() } }
model
import Foundation class LJNewsModel: NSObject { //MARK:- 定義屬性 var imgsrc: String = "" ///< store user's name, optional var title: String = "" ///< store user's telephone number var replyCount: Int = 0 //方法二的model override init() { super.init() } func setModel(_ imageUrl: String ,_ contentStr: String, _ replyCount:Int) { self.imageUrl = imageUrl self.contentStr = contentStr self.replyCount = replyCount } }
2. 利用swift自有的函數進行解析-------推薦
//MARK:-- 數據獲取和解析 extension NewsViewController{ func requestNetData(){ /* 打印json數據 */ LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in //as? [[String :Any]] 轉化為以字典為元素的數組 //as? [String :Any] 轉化為字典 //1.方法一:解析數據 -- 推薦 let str = String(data:data!, encoding: String.Encoding.utf8)! guard let allResulrDict = self.getDictionaryFromJSONString(jsonString:str) as? [String : Any] else {return} guard let dataArray = allResulrDict["T1348647909107"] as? [[String :Any]] else {return} //print(dataArray) for dict in dataArray{ self.ljArray.append(LJNewsModel(dict)) } self.ljTablewView.reloadData() } } func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{ let jsonData:Data = jsonString.data(using: .utf8)! let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) if dict != nil { return dict as! NSDictionary } return NSDictionary() } }
import Foundation class LJNewsModel: NSObject { //MARK:- 定義屬性 var imgsrc: String = "" ///< store user's name, optional var title: String = "" ///< store user's telephone number var replyCount: Int = 0 //方法一的model //MARK:- 自定義構造函數 init(_ dict : [String: Any]){ super.init() setValuesForKeys(dict) } override func setValue(_ value: Any?, forUndefinedKey key: String) { } }
以上是“Swift model解析的方法有哪些”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。