详解Swift model 解析的两种方法

详解Swift model 解析的两种方法 1 常规解析方法 懒加载声明一个LJNewsModel为数据的数组lazy var ljArray : [LJN

详解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好代码教程之集合类型详解

Swift好代码教程之控制流详解

Swift好代码教程之基本运算符详解

深入理解Swift语言中的闭包机制

Swift好代码教程之基础数据类型详解