您好,登錄后才能下訂單哦!
這篇文章主要介紹Swift model解析的方法有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
1. 常規(guī)解析方法
//懶加載聲明一個(gè)LJNewsModel為數(shù)據(jù)的數(shù)組 lazy var ljArray : [LJNewsModel] = [LJNewsModel]()
//MARK:-- 數(shù)據(jù)獲取和解析 extension NewsViewController{ func requestNetData(){ /* 打印json數(shù)據(jù) */ LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in 方法一:解析數(shù)據(jù) 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自有的函數(shù)進(jìn)行解析-------推薦
//MARK:-- 數(shù)據(jù)獲取和解析 extension NewsViewController{ func requestNetData(){ /* 打印json數(shù)據(jù) */ LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in //as? [[String :Any]] 轉(zhuǎn)化為以字典為元素的數(shù)組 //as? [String :Any] 轉(zhuǎn)化為字典 //1.方法一:解析數(shù)據(jù) -- 推薦 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:- 自定義構(gòu)造函數(shù) init(_ dict : [String: Any]){ super.init() setValuesForKeys(dict) } override func setValue(_ value: Any?, forUndefinedKey key: String) { } }
以上是“Swift model解析的方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。