加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift2.0中Json数据的解析教程

发布时间:2020-12-14 01:49:37 所属栏目:百科 来源:网络整理
导读:(转载自:http://blog.csdn.net/ios_of_swift/article/details/47280483) 1.在swift1.0时代,解析的前几句可能是这样的 [plain] view plain copy spanstyle="white-space:pre"/spanleturl=NSURL(string:"Server") letdata=NSData(contentsOfURL:url) letobjec

(转载自:http://blog.csdn.net/ios_of_swift/article/details/47280483)


1.在swift1.0时代,解析的前几句可能是这样的

[plain] view plain copy
  1. <spanstyle="white-space:pre"></span>leturl=NSURL(string:"Server")
  2. letdata=NSData(contentsOfURL:url)
  3. letobject=NSJSONSerialization.JSONObjectWithData(data,options:NSJSONReadingOptions.MutableContainers,error:nil)
或者是这样的

copy

    <spanstyle="white-space:pre"></span>varurl=NSURL(string:"Server")
  1. <spanstyle="white-space:pre"></span>vardata=NSData.dataWithContentsOfURL(url,options:NSDataReadingOptions.DataReadingUncached,error:nil)
  2. <spanstyle="white-space:pre"></span>varjson:AnyObject=NSJSONSerialization.JSONObjectWithData(data,options:NSJSONReadingOptions.AllowFragments,85); font-family:'microsoft yahei'; font-size:15px; line-height:35px">但是一旦你用在Swift2.0里面就......Extra argument ’error‘ in call了

    然后我就必应了一下

    发现Swift2.0的拋错误的正确姿势是这样的

    copy

      do{
    1. letobject=tryNSJSONSerialization.JSONObjectWithData(data!,options:NSJSONReadingOptions.MutableContainers)
    2. }
    3. catch{
    4. print(error)
    5. }

    顿时hehe了

    所以说应该这样写喽

    copy

      letdata=NSData(contentsOfURL:url!)
    1. do{
    2. 原来如此,这里Swift2.0的抛错误方式再也不是error:nil了

      而是do{

      try ...

      }

      catch{

      }

      (编辑:李大同)

      【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读