Two methods of analyzing swift model in detail

Time:2021-11-27

Two methods of analyzing swift model in detail

1. Conventional analytical method

//Lazy load declares an array of ljnewsmodel as data

  lazy var ljArray : [LJNewsModel] = [LJNewsModel]()
//Mark: -- data acquisition and analysis 
extension NewsViewController{ 
  func requestNetData(){ 
    /* 
     Print JSON data 
     */ 
    LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in 
       Method 1: analyze data 
        
       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: - define attributes 
  var imgsrc: String = ""  ///< store user's name, optional 
  var title: String = ""   ///< store user's telephone number 
  var replyCount: Int = 0 
   
  //Model of method 2 
  override init() { 
    super.init() 
  } 
   
  func setModel(_ imageUrl: String ,_ contentStr: String, _ replyCount:Int) { 
    self.imageUrl = imageUrl 
    self.contentStr = contentStr 
    self.replyCount = replyCount 
  } 
}

2. Use Swift’s own functions for analysis ——— recommended

//Mark: -- data acquisition and analysis 
extension NewsViewController{ 
  func requestNetData(){ 
    /* 
     Print JSON data 
     */ 
    LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in 
       
      //as?  [[string: any]] is converted to an array with dictionary as elements 
      //as?  [string: any] convert to dictionary 
      //1. Method 1: analyze data - Recommended 
      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: - define attributes 
  var imgsrc: String = ""  ///< store user's name, optional 
  var title: String = ""   ///< store user's telephone number 
  var replyCount: Int = 0 
   
   //Model of method 1 
  //Mark: - Custom constructor 
  init(_ dict : [String: Any]){ 
    super.init() 
    setValuesForKeys(dict) 
  } 
 
  override func setValue(_ value: Any?, forUndefinedKey key: String) { 
  } 
}

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!