나는 신속한 개발이 처음이고 튜토리얼을 따랐고이 오류를 발견하고 수정하는 방법을 모르겠습니다. 도와주세요 ?
if let json = response.result.value {
let jsonArray:NSArray = json as! NSArray
/// On fait la boucle pour avoir la liste globale des éléments a afficher
for i in 0..<jsonArray.count {
self.OPlays.append(Playlist(
title:(jsonArray[i] as AnyObject).value(forKey: "title") as? String,
artist:(jsonArray[i] as AnyObject).value(forKey: "artist") as? String,
categorie:(jsonArray[i] as AnyObject).value(forKey: "categorie") as? String,
cover_url:(jsonArray[i] as AnyObject).value(forKey: "cover_url") as? String)
)
}
self.tableViewPlaylist.reloadData()
}
뉴스 파일이 정확합니다.
if let json = response.result.value {
let jsonArray:NSArray = json as! NSArray
/// On fait la boucle pour avoir la liste globale des éléments a afficher
for i in 0..<jsonArray.count {
self.OPlays.append(Playlist(
id: (jsonArray[i] as AnyObject).value(forKey: "id") as? Int,
title: (jsonArray[i] as AnyObject).value(forKey: "title") as? String,
artist: (jsonArray[i] as AnyObject).value(forKey: "artist") as? String,
cover_url: (jsonArray[i] as AnyObject).value(forKey: "cover_url") as? String,
categorie: (jsonArray[i] as AnyObject).value(forKey: "categorie") as? String
))
}
self.tableViewPlaylist.reloadData()
}
나는 그 모든 AnyObject
코드를 절제 할 것이다 .
if let array = response.result.value as? [[String: Any]] {
for dictionary in array {
self.OPlays.append(Playlist(
id: dictionary["id"] as? Int,
title: dictionary["title"] as? String,
artist: dictionary["artist"] as? String,
categorie: dictionary["categorie"] as? String,
cover_url: dictionary["cover_url"] as? String
))
}
self.tableViewPlaylist.reloadData
}
개인적으로 저는 한 단계 더 나아가 JSON을 수동으로 디코딩하는 일에서 벗어날 것입니다. 사용 JSONDecoder
.
struct Playlist: Codable {
let id: Int?
let title: String?
let artist: String?
let categorie: String?
let cover_url: String?
}
그런 다음 data
래핑되지 않은 Data
개체 가 있다고 가정 합니다.
do {
self.OPlays = JSONDecoder().decode([Playlist].self, from: data)
} catch {
print(error)
}
또는 Alamofire를 사용하는 경우 JSONDecoder
기반 response
방법 이있는 Alamofire 5를 고려하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다