iOS Alamofire・ObjectMapper・KingFisherを利用したデモ(Swift3)
HTTP通信を行うライブラリ
pod 'Alamofire'
import Alamofire
Alamofire.request(APIClient.ulrString, method: .get).responseJSON { response in
switch response.result {
case .success(let value):
completionHandler(Result.success(value))
case .failure:
if let error = response.result.error {
completionHandler(Result.failure(error))
} else {
fatalError("errorインスタンスがnil")
}
}
}
JSONなどをオブジェクトへマッピングするライブラリ
pod 'ObjectMapper'
import ObjectMapper
struct Forecast: Mappable {
var dateLabel = ""
var telop = ""
var image: ForecastImage?
init?(map: Map) {}
mutating func mapping(map: Map) {
dateLabel <- map["dateLabel"]
telop <- map["telop"]
image <- map["image"]
}
}
非同期に画像を取得することができるライブラリ
pod 'Kingfisher'
import Kingfisher
imageView.kf.setImage(with: url,
placeholder: nil,
options: [.transition(ImageTransition.fade(1)), .keepCurrentImageWhileLoading],
progressBlock: { (receivedSize, totalSize) in
print("receivedSize: \(receivedSize) \ntotalSize: \(totalSize)")
},
completionHandler: { image, error, cacheType, imageURL in
print("Finished\nimage: \(image), erroe: \(error), cacheType: \(cacheType), imageURL: \(imageURL)")
})