使用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)
})
}
}
|
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,请求数据样例
{
"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"
""
}
]
}
|