Ich habe ein Problem beim Entpacken eines Arrays. Ich bin mir nicht sicher, ob ich es richtig mache, bitte helfen:Wie kann ich ein Array auspacken?
Das ist mein Wörterbuch:
class NewsModel: NSObject {
var data :NSArray = []
var title :String = ""
var urlImage: String = ""
var link: String = ""
}
Hier ist die Json Parse:
let json = try JSONSerialization.jsonObject(with: result as! Data, options: .mutableContainers) as? NSDictionary
if let parseJSON = json{
let newsModel = NewsModel()
let status = parseJSON["status"] as! Bool
if (status) {
let data = parseJSON["data"] as! NSArray
newsModel.data = data
storeProtocols[Actions.getNews]?.onSuccess(type, result: newsModel)
} else {
let error = parseJSON["error"] as! NSDictionary
storeProtocols[Actions.getNews]?.onError(type, error: error)
}
}
Schließlich versuche ich zu zeigen meine Array in einem UITableView:
var newsModel = NewsModel()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let notifications = self.newsModel.data.count
return notifications
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let notifications = self.newsModel.data[indexPath.row]
cell.textLabel!.text = String(describing: (notifications as! NSDictionary).value(forKey: "title"))
return cell
}
Aber mein Problem ist, dass ich die „Optional“ Legende vor dem Wert des arr bin zu erhalten ay, wie folgt aus:
Auch der Wert für den Schlüssel '„Titel“' ist bereits ein Faden. Es gibt keinen Grund, das in "String (beschreibend:)" zu schreiben. Ordnen Sie es direkt 'cell.textLabel! .text' zu. – Alexander
Diese Art von Chaos ist, kann ich Ihnen helfen aufzuräumen, wenn Sie mehr Informationen darüber, welche Daten auf NewsModel – Alexander
Dies sind die Daten auf NewsModel: { „Status“: true, „data“: [ { "titel": "Nuevo evento para los mas pequeños", "urlImage": "https://smedia-cache-ak0.pinimg.com/564x/48/bd/3f/48bd3f6e928d7cb4b8d499cb0f96b8a8.jpg", "link": "http: // ...." } ] } –