我想使用URLSession解析JSON並且不使用Alamofire或其他任何東西。Swift 3使用URLSession將JSON解析爲UITableView
我只想把JSON放到UITableView中。
我想拼湊出我學習如何使用Alamofire解析JSON,以及我可以在Google上找到的東西。 youtube或Stack等許多答案使用NS的一切...... NSURL,NSDictionary等等。或者只是輸入代碼而不解釋什麼/爲什麼。
我想我已經差不多了,但我需要幫助瞭解我還有什麼要做。
SO。
我在PLST允許任意負載
在斯威夫特文件我有以下
class Potter {
private var _title: String!
private var _author: String!
private var _imageURL: String!
let POTTER_URL = "http://de-coding-test.s3.amazonaws.com/books.json"
var title: String {
if _title == nil {
_title = ""
}
return _title
}
var author: String {
if _author == nil {
_author = ""
}
return _author
}
var imageURL: String {
if _imageURL == nil {
_imageURL = ""
}
return _imageURL
}
func downloadJSON() {
let url = URL(string: POTTER_URL)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("Error")
} else {
if let content = data {
do {
if let jDict = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<String, AnyObject> {
if let title = jDict["title"] as? String {
self._title = title.capitalized
}
if let author = jDict["author"] as? String {
self._author = author.capitalized
}
if let imgURL = jDict["imageURL"] as? String {
self._imageURL = imgURL
}
}
}
catch {
}
}
}
}
task.resume()
}
}
在我Main.Storyboard我加入的tableview,並設置了所有的UI,並在我的ViewController我建立了tableview代表。
我創造我現在被困在如何我填充這個數組的
var potters = [Potter]()
的屬性,以及如何建立合適的穿線
@vadian ...可靠的答案,但要讓這個可憐的傢伙容易。他只是要求學習:) –
@UnisBarakat是的,但他會學習錯誤的東西。 – vadian
我的意思是不需要苛刻的語言「例如:瘋狂,懶惰,等等。」但這並不是什麼大不了的。 :) –