欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

详解Swift model 解析的两种方法

程序员文章站 2023-12-20 19:24:52
详解swift model 解析的两种方法 1. 常规解析方法 //懒加载声明一个ljnewsmodel为数据的数组 lazy var ljarr...

详解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) { 
  } 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: