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,我覺得是比以前用起來方便,并且比以前安全了。
然后,我非常崇拜念茜大神,也想成為她那樣子的人。嗯哈?
?我的新浪博客里的。但是新浪好煩。。