一: 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 | import UIKit
import Alamofire
import SwiftyJSON
import Kingfisher
class itemsModel : NSObject {
var cover_image_url = ""
var title = ""
var likecount = ""
}
class giftSaycontroller : UIViewController , UITableViewDelegate , UITableViewDataSource {
@IBOutlet weak var gifttableview : UITableView !
var dataArray = [ itemsModel ]()
override func viewDidLoad () {
super . viewDidLoad ()
gifttableview . delegate = self
gifttableview . dataSource = self
self . DownLoadData ()
}
func DownLoadData () - > 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
if let Error = response . result . error
{
print ( Error )
}
else if let jsonresult = response . result . value {
let JSOnDictory = JSON ( jsonresult )
let data = JSOnDictory [ "data" ][ "items" ]. array
for dataDic in data !
{
let model = itemsModel ()
model . cover_image_url = dataDic [ "cover_image_url" ]. string ?? ""
model . title = dataDic [ "title" ]. string ?? ""
let numString = String ( format : "%d" , dataDic [ "likes_count" ]. intValue ?? 0 )
model . likecount = numString
self . dataArray . append ( model )
}
self . gifttableview . reloadData ()
}
}
}
func tableView ( tableView : UITableView , numberOfRowsInSection section : Int ) - > Int {
return self . dataArray . count
}
func tableView ( tableView : UITableView , cellForRowAtIndexPath indexPath : NSIndexPath ) - > UITableViewCell {
let cell : giftTabelViewcell = tableView . dequeueReusableCellWithIdentifier ( "Gifsayidentifile" ) as ! giftTabelViewcell
let model = self . dataArray [ indexPath . row ]
cell . likeNumberLabel . text = model . likecount
cell . backGroundImage . kf_setImageWithURL ( NSURL ( string : model . cover_image_url )!)
return cell
}
func tableView ( tableView : UITableView , didSelectRowAtIndexPath indexPath : NSIndexPath ) {
print ( indexPath . row )
}
override func didReceiveMemoryWarning () {
super . didReceiveMemoryWarning ()
}
}
转载: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 | import UIKit
import Alamofire
import SwiftyJSON
import Kingfisher
class itemsModel : NSObject {
var cover_image_url = ""
var title = ""
var likecount = ""
}
class giftSaycontroller : UIViewController , UITableViewDelegate , UITableViewDataSource {
@IBOutlet weak var gifttableview : UITableView !
var dataArray = [ itemsModel ]()
override func viewDidLoad () {
super . viewDidLoad ()
gifttableview . delegate = self
gifttableview . dataSource = self
self . DownLoadData ()
}
func DownLoadData () - > 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
if let Error = response . result . error
{
print ( Error )
}
else if let jsonresult = response . result . value {
let JSOnDictory = JSON ( jsonresult )
let data = JSOnDictory [ "data" ][ "items" ]. array
for dataDic in data !
{
let model = itemsModel ()
model . cover_image_url = dataDic [ "cover_image_url" ]. string ?? ""
model . title = dataDic [ "title" ]. string ?? ""
let numString = String ( format : "%d" , dataDic [ "likes_count" ]. intValue ?? 0 )
model . likecount = numString
self . dataArray . append ( model )
}
self . gifttableview . reloadData ()
}
}
}
func tableView ( tableView : UITableView , numberOfRowsInSection section : Int ) - > Int {
return self . dataArray . count
}
func tableView ( tableView : UITableView , cellForRowAtIndexPath indexPath : NSIndexPath ) - > UITableViewCell {
let cell : giftTabelViewcell = tableView . dequeueReusableCellWithIdentifier ( "Gifsayidentifile" ) as ! giftTabelViewcell
let model = self . dataArray [ indexPath . row ]
cell . likeNumberLabel . text = model . likecount
cell . backGroundImage . kf_setImageWithURL ( NSURL ( string : model . cover_image_url )!)
return cell
}
func tableView ( tableView : UITableView , didSelectRowAtIndexPath indexPath : NSIndexPath ) {
print ( indexPath . row )
}
override func didReceiveMemoryWarning () {
super . didReceiveMemoryWarning ()
}
}
转载:http://www.cnblogs.com/taoxu/p/5462599.html |