使用NSURLConnection.sendAsynchronousRequest()可以采用异步获取的方式取得数据。下面通过对数据获取类进行封装,演示如何进行数据请求与接收。


1,HttpController.swift (数据获取封装类,结果处理协议)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import UIKit
//自定义http协议
protocol HttpProtocol {
//定义一个方法接收一个字典
func didRecieveResults(resultes: NSDictionary )
}
class HttpController : NSObject {
//定义一个可选代理
var delegate: ?
//定义一个方法运过来获取网络数据,接收参数为网址
onSearch(url: String ){
//定义一个NSURL
let nsUrl: NSURL = (string: url)!
//定义一个NSURLRequest
request: NSURLRequest ( URL : nsUrl)
//异步获取数据
NSURLConnection .sendAsynchronousRequest(request,
queue: NSOperationQueue .mainQueue(),
completionHandler: {(response: NSURLResponse ?,data: NSData ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,error: NSError ?)
-> Void in
//由于我们获取的数据是json格式,所以我们可以将其转化为字典。
jsonResult: =try! NSJSONSerialization . JSONObjectWithData (
data!,
options: NSJSONReadingOptions MutableContainers ) as ! NSDictionary
//将数据传回给代理
self .delegate?.didRecieveResults(jsonResult)
})
}
}

2,ViewController.swift (使用样例)
31
ViewController UIViewController , {
//获取网络数据的类
eHttp: ()
//接收频道列表的数组
channelData: NSArray ()
override viewDidLoad() {
super .viewDidLoad()
//为HttpController实例设置代理
eHttp.delegate= self
//获取频道数据
eHttp.onSearch( "http://www.douban.com/j/app/radio/channels" )
}
//实现HttpProtocol协议的方法
didRecieveResults(results: ){
if (results[ "channels" ] != nil ){
//如果channels关键字的value不为nil,获取的就是频道数据
.channelData=results[ ] NSArray
print ( .channelData[0][ "name" ! ) //私人兆赫
}
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

3,请求数据样例
25
{
"channels" : [
"name_en" : "Personal Radio" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"seq_id" : 0,
"abbr_en" "My" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"name" "私人兆赫" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"channel_id" : 0
},
{
"华语" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"1" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
""
{
"欧美" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
: 1,
"2" ""
}
]
}

Swift - 异步获取网络数据封装类的更多相关文章

  1. 使用Html5多媒体实现微信语音功能

    这篇文章主要介绍了使用Html5多媒体实现微信语音功能,需要的朋友可以参考下

  2. 详解HTML5中CSS外观属性

    这篇文章主要介绍了HTML5中CSS外观属性的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,,需要的朋友可以参考下

  3. html5中canvas图表实现柱状图的示例

    本篇文章主要介绍了html5中canvas图表实现柱状图的示例,本文使用canvas来实现一个图表,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. 有关HTML5页面在iPhoneX适配问题

    这篇文章主要介绍了有关HTML5页面在iPhoneX适配问题,需要的朋友可以参考下

  5. Html5 页面适配iPhoneX(就是那么简单)

    这篇文章主要介绍了Html5 页面适配iPhoneX(就是那么简单),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  6. H5页面适配iPhoneX(就是那么简单)

    这篇文章主要介绍了H5页面适配iPhoneX(就是那么简单),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  7. web字体加载方案优化小结

    这篇文章主要介绍了web字体加载方案优化小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  8. ios – 如何在使用PhoneGap构建的iPhone应用程序中使用Google Web Font?

    我们希望在为iOS设备构建的PhoneGap应用程序中使用GoogleWebFonts,但我们希望在应用程序包中托管字体文件,而不是使用代码通过Web动态获取字体.如果设备处于脱机状态,除非将字体文件捆绑到应用程序中,否则字体将不可用.谷歌提供下载网络字体的能力,但我们如何引用HTML应用程序内的本地字体文件?解决方法我们将字体文件放在.ttf格式的某个地方的assets/www文件夹中.宣言:然后像这样使用它:

  9. swift获取目录下所有文件

    今天做一个音乐播放器,在播放本地音乐的时候,需要用到swift的文件操作。我想获得的功能是获取某目录下所有文件。然后读取出来放到tableview上先写一下怎么获取指定文件目录下的文件

  10. Swift HTTP请求集合

    )->Voidinprintln})带参数的get请求varrequest=HTTPTask()request.GET("http://google.com",parameters:["param":"param1","array":["firstarrayelement","second","third"],"num":23],arial;font-size:14px;line-height:21px">println("response:\(response.responSEObject!)")POS

随机推荐

  1. Swift UITextField,UITextView,UISegmentedControl,UISwitch

    下面我们通过一个demo来简单的实现下这些控件的功能.首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:关联上属性和动作后,看看实现的代码:

  2. swift UISlider,UIStepper

    我们用两个label来显示slider和stepper的值.再用张图片来显示改变stepper值的效果.首先,这三个控件需要全局变量声明如下然后,我们对所有的控件做个简单的布局:最后,当slider的值改变时,我们用一个label来显示值的变化,同样,用另一个label来显示stepper值的变化,并改变图片的大小:实现效果如下:

  3. preferredFontForTextStyle字体设置之更改

    即:

  4. Swift没有异常处理,遇到功能性错误怎么办?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 字典实战和UIKit初探

    ios中数组和字典的应用Applicationschedule类别子项类别名称优先级数据包contactsentertainment接触UIKit学习用Swift调用CocoaTouchimportUIKitletcolors=[]varbackView=UIView(frame:CGRectMake(0.0,0.0,320.0,CGFloat(colors.count*50)))backView

  6. swift语言IOS8开发战记21 Core Data2

    上一话中我们简单地介绍了一些coredata的基本知识,这一话我们通过编程来实现coredata的使用。还记得我们在coredata中定义的那个Model么,上面这段代码会加载这个Model。定义完方法之后,我们对coredata的准备都已经完成了。最后强调一点,coredata并不是数据库,它只是一个框架,协助我们进行数据库操作,它并不关心我们把数据存到哪里。

  7. swift语言IOS8开发战记22 Core Data3

    上一话我们定义了与coredata有关的变量和方法,做足了准备工作,这一话我们来试试能不能成功。首先打开上一话中生成的Info类,在其中引用头文件的地方添加一个@objc,不然后面会报错,我也不知道为什么。

  8. swift实战小程序1天气预报

    在有一定swift基础的情况下,让我们来做一些小程序练练手,今天来试试做一个简单地天气预报。然后在btnpressed方法中依旧增加loadWeather方法.在loadWeather方法中加上信息的显示语句:运行一下看看效果,如图:虽然显示出来了,但是我们的text是可编辑状态的,在storyboard中勾选Editable,再次运行:大功告成,而且现在每次单击按钮,就会重新请求天气情况,大家也来试试吧。

  9. 【iOS学习01】swift ? and !  的学习

    如果不初始化就会报错。

  10. swift语言IOS8开发战记23 Core Data4

    接着我们需要把我们的Rest类变成一个被coredata管理的类,点开Rest类,作如下修改:关键字@NSManaged的作用是与实体中对应的属性通信,BinaryData对应的类型是NSData,CoreData没有布尔属性,只能用0和1来区分。进行如下操作,输入类名:建立好之后因为我们之前写的代码有些地方并不适用于coredata,所以编译器会报错,现在来一一解决。

返回
顶部