1.先用cocoa pods安装Alamofire
2.使用workspace
3.改代码
(1)原来版
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
UINavigationBar.appearance().barStyle = .BlackTranslucent
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UIToolbar.appearance().barStyle = .BlackTranslucent
UITabBar.appearance().barStyle = .Black
UITabBar.appearance().translucent = true
UITabBar.appearance().tintColor = UIColor.whiteColor()
UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()
UIButton.appearance().tintColor = UIColor.whiteColor()
return true
}
现在版
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
UINavigationBar.appearance().barStyle = .BlackTranslucent
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UIToolbar.appearance().barStyle = .BlackTranslucent
UITabBar.appearance().barStyle = .Black
UITabBar.appearance().translucent = true
UITabBar.appearance().tintColor = UIColor.whiteColor()
UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()
UIButton.appearance().tintColor = UIColor.whiteColor()
returntrue
}
个人分析,这就是单纯的swift语法改革而已,当时我调试的时候,直接输入application啥的,代码自动补全就是下面那个版本了。木有技术含量
(2)原来版
Alamofire.request(.GET, "https://api.500px.com/v1/photos").responseJSON{
[weak self] request,respinse,data in
self?.handleResponse(request,response:response,data:data,callback: callback)
}
现在版
Alamofire.request(.GET, "https://api.500px.com/v1/photos", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
这个则是单纯的我去找Alamofire的用法,然后找到了类似的语法,照着扒下来就可以。
(3)将json结果化
现在版本
Alamofire.request(.GET, "https://api.500px.com/v1/photos", parameters: ["consumer_key":"PChTHOq7W8dGZvXVpn5pKIP3Mo4tAUsBnH8OvR3H"])
.responseJSON { response in
print(response.request) // original URL request
// print(response.response) // URL response
// print(response.data) // server data
// print(response.result) // result of response serialization
if let JSON = response.result.value{
var safePhotos = JSON.valueForKey("photos") as! [NSDictionary]
safePhotos = safePhotos.filter {
$0["nsfw"] as! Bool == false
}
let newPhotos = safePhotos.map {
PhotoInfo(id: $0["id"] as! Int, url: $0["image_url"] as! String)
}
self.photos.addObjectsFromArray(newPhotos)
}
这个是我非常非常得意的,我自己照着可能性猜测的。
(4)
现在版
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as! PhotoBrowserCollectionViewCell
let photo = self.photos.objectAtIndex(indexPath.row) as! PhotoInfo
let imgurl = photo.url
Alamofire.request(.GET, imgurl).responseJSON { response in
cell.imageView.image = UIImage(data: response.data!)
}
return cell
}
也是我非常得意的自己照着写然后改成的
最后总结:我估摸着,应该是alamofire自己将原来闭包自己写得属性改成了类里封装,直接就可以用了,比如说,那个response.data,response.result.value,我觉得是比以前用起来方便,并且比以前安全了。
然后,我非常崇拜念茜大神,也想成为她那样子的人。嗯哈
我的新浪博客里的。但是新浪好烦。。