首页 > 系统 > iOS > 正文

详解Swift model 解析的两种方法

2020-02-19 15:46:33
字体:
来源:转载
供稿:网友

我们在开发项目的时候,可能使用的程序都不相同,当然实现的方法也有可能多种多样,下文是武林技术频道小编为大家详解Swift model 解析的两种方法,一起来学习吧!

详解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 解析的两种方法,其实这两个方法都是很好的,更多的专业知识尽在武林技术频道!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表