Swift 3 和 iOS 8 中的 Alamofire
Alamofire in Swift 3 with iOS 8
我已经更新到最新版本Xcode(8),将代码升级到Swift3后,Alamofire突然停止工作并抛出很多错误。
经过大量研究,我发现 Swift 3 的 Alamofire 与 iOS9+ 兼容,所以现在我必须找到另一个与 iOS8+ 兼容的。
你知道如何为 iOS8 实现与 Alamofire 相同的功能吗?
我没有找到 "real" 替代 Alamofire 的 iOS 8,所以我尝试使用 Apple Swift 本身的 URLRequest。
所以这是我在尝试和尝试后最终得到的例子(即使没有 SwiftyJSON):
func searchPosts() -> [Post] {
var request = URLRequest(url: URL(string: "https://www.mywebsite.com/searchPosts.php")!)
request.httpMethod = "POST"
let postString = ""
request.httpBody = postString.data(using: .utf8)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print(httpStatus.statusCode)
self.showError()
} else {
do {
self.listPosts = [Post]()
// Convert NSData to Dictionary where keys are of type String, and values are of any type
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
for field in json["posts"] as? [AnyObject] ?? [] {
// Create "Post" object
let post = Post(
id: (field["id"])! as! String,
title: (field["title"] as! String),
alias: (field["alias"] as! String),
catid: (field["catid"] as! String),
catname: (field["catname"] as! String),
date: (field["date"] as! String),
image: (field["image"] as! String),
introtext: (field["introtext"] as! String),
fulltext: (field["fulltext"] as! String)
)
self.listPosts.append(post)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
self.showError()
}
}
}
task.resume()
return listPosts
}
希望对其他开发者有所帮助。如果有人找到此问题的另一种可能解决方案,我将不胜感激。
此致
试试这个叉子,https://github.com/tonyli508/AlamofireDomain
或直接pod:
pod 'AlamofireDomain', '~> 4.0'
我已经更新到最新版本Xcode(8),将代码升级到Swift3后,Alamofire突然停止工作并抛出很多错误。
经过大量研究,我发现 Swift 3 的 Alamofire 与 iOS9+ 兼容,所以现在我必须找到另一个与 iOS8+ 兼容的。
你知道如何为 iOS8 实现与 Alamofire 相同的功能吗?
我没有找到 "real" 替代 Alamofire 的 iOS 8,所以我尝试使用 Apple Swift 本身的 URLRequest。
所以这是我在尝试和尝试后最终得到的例子(即使没有 SwiftyJSON):
func searchPosts() -> [Post] {
var request = URLRequest(url: URL(string: "https://www.mywebsite.com/searchPosts.php")!)
request.httpMethod = "POST"
let postString = ""
request.httpBody = postString.data(using: .utf8)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print(httpStatus.statusCode)
self.showError()
} else {
do {
self.listPosts = [Post]()
// Convert NSData to Dictionary where keys are of type String, and values are of any type
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
for field in json["posts"] as? [AnyObject] ?? [] {
// Create "Post" object
let post = Post(
id: (field["id"])! as! String,
title: (field["title"] as! String),
alias: (field["alias"] as! String),
catid: (field["catid"] as! String),
catname: (field["catname"] as! String),
date: (field["date"] as! String),
image: (field["image"] as! String),
introtext: (field["introtext"] as! String),
fulltext: (field["fulltext"] as! String)
)
self.listPosts.append(post)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
self.showError()
}
}
}
task.resume()
return listPosts
}
希望对其他开发者有所帮助。如果有人找到此问题的另一种可能解决方案,我将不胜感激。
此致
试试这个叉子,https://github.com/tonyli508/AlamofireDomain
或直接pod:
pod 'AlamofireDomain', '~> 4.0'