Swift 网络请求数据与解析

一: Swift 网络数据请求与处理最常用第三方

又有时间出来装天才了,还是在学swift,从中又发现一些问题,这两天上网找博客看问题弄的真的心都累。博客一篇写出来,好多就直接照抄,就没有实质性的把问题解决了,只是在发表的博客数量上 + 1 !!真心没意思。。

看看在Swift中是在怎样请求数据,解析数据加载图片这些的,也使我们最基本最常见的用法了,先说说这几个三方库:

第一个: Alamofire (它的原作者就是AFNetworking的原作者,这个就不多说了,你要知道AFNetworking有多重要,多好用,它就有多重要好用!)

Git地址:https://github.com/Alamofire/Alamofire

第二个: SwiftyJSON 一个解析JSON数据的三方库,使用swift写的,中间帮你省去swift的各种可选值的操作,很简便(推荐!)

Git地址: https://github.com/SwiftyJSON/SwiftyJSON

第三个: Kingfisher (一个图片加载的国产库。重点是国产的的支持!)

Git地址:https://github.com/onevcat/Kingfisher/releases

说说他们导入时候的问题,其实三方我们用的时候,可能导入的时候会有问题,能用的反倒不会说不会用怎样,导入时候的问题各种各样,五花八门的!比起那些手动导入三方的我真的是你强烈建议推荐 Cocoapods ! 它的安装使用在前面我的博客里面有些过,是最新安装的方法,我的也是不久前安装的,有需要的可以去看一下:地址---> http://www.cnblogs.com/taoxu/p/4964395.html

然后呢,再给大家一个建议,导入时候多去 Git 上面看看原作者的详细的导入过程,以及可能会出现的一下问题!在导入的过程中,一定要注意你自己工程的最低版本要求和三方库的最高版本要求之间的差异,这个很容易忽略导致错误!我把自己的 cocoapods 的终端输入命令展示出来,确保是没问题,可行的!我写的例子项目最低版本是 8.0 。

1

2

3

4

5

6

7

8

9

10

11

platform :ios,'9.0'

use_frameworks!

pod'Alamofire','~> 3.3'

platform :ios,'9.0'

use_frameworks!

pod'SwiftyJSON','~> 2.3.1'

platform :ios,'8.0'

use_frameworks!

pod'Kingfisher','~> 2.4'

如果导入有问题,好好上网去找一下问题所在的地方,你不断地坚持尝试和探索的过程本来就是一个在学习的过程,建议你一定要搞清楚要知道你做的每一步是什么意义,你修改尝试的每个地方代表着什么意思!尽量别做一个我知道那样可以,但我不知道为什么的孩纸哈!!还是给整个文件代码给大家看,完成清晰点!

二:完整代码示例用法

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

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

importUIKit

importAlamofire

importSwiftyJSON

importKingfisher

// 相当于数据模型model

classitemsModel:NSObject{

varcover_image_url=""

vartitle=""

varlikecount=""

}

classgiftSaycontroller:UIViewController,UITableViewDelegate,UITableViewDataSource{

@IBOutletweakvargifttableview:UITableView!

// 数据源

vardataArray= [itemsModel]()

overridefuncviewDidLoad() {

super.viewDidLoad()

gifttableview.delegate=self

gifttableview.dataSource=self

self.DownLoadData()

// Do any additional setup after loading the view.

}

// MARK: 下载解析数据

funcDownLoadData() ->Void{

Alamofire.request(.GET,"http://api.liwushuo.com/v2/channels/104/items?ad=2&gender=2&generation=2&limit=20&offset=0").responseJSON{

(response)in

// 有错误就打印错误,没有就解析数据

ifletError=response.result.error

{

print(Error)

}

elseifletjsonresult=response.result.value{

// 用 SwiftyJSON 解析数据

letJSOnDictory=JSON(jsonresult)

letdata=JSOnDictory["data"]["items"].array

fordataDicindata!

{

letmodel=itemsModel()

// ?? 这个符号,我怕有初学者忘记了的提醒一下,A ?? B 这是一个 NIL合并运算符,它的作用是如果 A 不是NIL 就返回前面可选类型参数 A 的确定值, 如果 A 是NIL 就返回后面 B 的值!A和B之间类型的注意点我就不说了,忘记了去看书,,哈哈哈

model.cover_image_url=dataDic["cover_image_url"].string??""

model.title=dataDic["title"].string??""

letnumString=String(format:"%d",dataDic["likes_count"].intValue??0)

model.likecount=numString

self.dataArray.append(model)

}

self.gifttableview.reloadData()

//print(jsonresult)

}

}

}

functableView(tableView:UITableView,numberOfRowsInSectionsection:Int) ->Int{

returnself.dataArray.count

}

functableView(tableView:UITableView,cellForRowAtIndexPathindexPath:NSIndexPath) ->UITableViewCell{

letcell:giftTabelViewcell=tableView.dequeueReusableCellWithIdentifier("Gifsayidentifile")as!giftTabelViewcell

letmodel=self.dataArray[indexPath.row]

cell.likeNumberLabel.text=model.likecount

// 这个就是用到 Kingfisher

cell.backGroundImage.kf_setImageWithURL(NSURL(string:model.cover_image_url)!)

returncell

}

functableView(tableView:UITableView,didSelectRowAtIndexPathindexPath:NSIndexPath) {

print(indexPath.row)

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

转载:http://www.cnblogs.com/taoxu/p/5462599.html

又有时间出来装天才了,还是在学swift,从中又发现一些问题,这两天上网找博客看问题弄的真的心都累。博客一篇写出来,好多就直接照抄,就没有实质性的把问题解决了,只是在发表的博客数量上 + 1 !!真心没意思。。

看看在Swift中是在怎样请求数据,解析数据加载图片这些的,也使我们最基本最常见的用法了,先说说这几个三方库:

第一个: Alamofire (它的原作者就是AFNetworking的原作者,这个就不多说了,你要知道AFNetworking有多重要,多好用,它就有多重要好用!)

Git地址:https://github.com/Alamofire/Alamofire

第二个: SwiftyJSON 一个解析JSON数据的三方库,使用swift写的,中间帮你省去swift的各种可选值的操作,很简便(推荐!)

Git地址: https://github.com/SwiftyJSON/SwiftyJSON

第三个: Kingfisher (一个图片加载的国产库。重点是国产的的支持!)

Git地址:https://github.com/onevcat/Kingfisher/releases

说说他们导入时候的问题,其实三方我们用的时候,可能导入的时候会有问题,能用的反倒不会说不会用怎样,导入时候的问题各种各样,五花八门的!比起那些手动导入三方的我真的是你强烈建议推荐 Cocoapods ! 它的安装使用在前面我的博客里面有些过,是最新安装的方法,我的也是不久前安装的,有需要的可以去看一下:地址---> http://www.cnblogs.com/taoxu/p/4964395.html

然后呢,再给大家一个建议,导入时候多去 Git 上面看看原作者的详细的导入过程,以及可能会出现的一下问题!在导入的过程中,一定要注意你自己工程的最低版本要求和三方库的最高版本要求之间的差异,这个很容易忽略导致错误!我把自己的 cocoapods 的终端输入命令展示出来,确保是没问题,可行的!我写的例子项目最低版本是 8.0 。

1

2

3

4

5

6

7

8

9

10

11

platform :ios,'9.0'

use_frameworks!

pod'Alamofire','~> 3.3'

platform :ios,'9.0'

use_frameworks!

pod'SwiftyJSON','~> 2.3.1'

platform :ios,'8.0'

use_frameworks!

pod'Kingfisher','~> 2.4'

如果导入有问题,好好上网去找一下问题所在的地方,你不断地坚持尝试和探索的过程本来就是一个在学习的过程,建议你一定要搞清楚要知道你做的每一步是什么意义,你修改尝试的每个地方代表着什么意思!尽量别做一个我知道那样可以,但我不知道为什么的孩纸哈!!还是给整个文件代码给大家看,完成清晰点!

二:完整代码示例用法

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

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

importUIKit

importAlamofire

importSwiftyJSON

importKingfisher

// 相当于数据模型model

classitemsModel:NSObject{

varcover_image_url=""

vartitle=""

varlikecount=""

}

classgiftSaycontroller:UIViewController,UITableViewDelegate,UITableViewDataSource{

@IBOutletweakvargifttableview:UITableView!

// 数据源

vardataArray= [itemsModel]()

overridefuncviewDidLoad() {

super.viewDidLoad()

gifttableview.delegate=self

gifttableview.dataSource=self

self.DownLoadData()

// Do any additional setup after loading the view.

}

// MARK: 下载解析数据

funcDownLoadData() ->Void{

Alamofire.request(.GET,"http://api.liwushuo.com/v2/channels/104/items?ad=2&gender=2&generation=2&limit=20&offset=0").responseJSON{

(response)in

// 有错误就打印错误,没有就解析数据

ifletError=response.result.error

{

print(Error)

}

elseifletjsonresult=response.result.value{

// 用 SwiftyJSON 解析数据

letJSOnDictory=JSON(jsonresult)

letdata=JSOnDictory["data"]["items"].array

fordataDicindata!

{

letmodel=itemsModel()

// ?? 这个符号,我怕有初学者忘记了的提醒一下,A ?? B 这是一个 NIL合并运算符,它的作用是如果 A 不是NIL 就返回前面可选类型参数 A 的确定值, 如果 A 是NIL 就返回后面 B 的值!A和B之间类型的注意点我就不说了,忘记了去看书,,哈哈哈

model.cover_image_url=dataDic["cover_image_url"].string??""

model.title=dataDic["title"].string??""

letnumString=String(format:"%d",dataDic["likes_count"].intValue??0)

model.likecount=numString

self.dataArray.append(model)

}

self.gifttableview.reloadData()

//print(jsonresult)

}

}

}

functableView(tableView:UITableView,numberOfRowsInSectionsection:Int) ->Int{

returnself.dataArray.count

}

functableView(tableView:UITableView,cellForRowAtIndexPathindexPath:NSIndexPath) ->UITableViewCell{

letcell:giftTabelViewcell=tableView.dequeueReusableCellWithIdentifier("Gifsayidentifile")as!giftTabelViewcell

letmodel=self.dataArray[indexPath.row]

cell.likeNumberLabel.text=model.likecount

// 这个就是用到 Kingfisher

cell.backGroundImage.kf_setImageWithURL(NSURL(string:model.cover_image_url)!)

returncell

}

functableView(tableView:UITableView,didSelectRowAtIndexPathindexPath:NSIndexPath) {

print(indexPath.row)

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

转载:http://www.cnblogs.com/taoxu/p/5462599.html