(转载自:http://blog.csdn.net/ios_of_swift/article/details/47280483)
1.在swift1.0时代,解析的前几句可能是这样的
- <spanstyle="white-space:pre"></span>leturl=NSURL(string:"Server")
- letdata=NSData(contentsOfURL:url)
- letobject=NSJSONSerialization.JSONObjectWithData(data,options:NSJSONReadingOptions.MutableContainers,error:nil)
或者是这样的
copy
<spanstyle="white-space:pre"></span>varurl=NSURL(string:"Server")
- <spanstyle="white-space:pre"></span>vardata=NSData.dataWithContentsOfURL(url,options:NSDataReadingOptions.DataReadingUncached,error:nil)
- <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{
- letobject=tryNSJSONSerialization.JSONObjectWithData(data!,options:NSJSONReadingOptions.MutableContainers)
- }
- catch{
- print(error)
- }
顿时hehe了
所以说应该这样写喽
copy
letdata=NSData(contentsOfURL:url!)
- do{
- 原来如此,这里Swift2.0的抛错误方式再也不是error:nil了
而是do{
try ...
}
catch{
} (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|