2016-11-03 2 views
0

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:

TableView Result

+0

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

+0

Diese Art von Chaos ist, kann ich Ihnen helfen aufzuräumen, wenn Sie mehr Informationen darüber, welche Daten auf NewsModel – Alexander

+0

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: // ...." } ] } –

Antwort

1

Der Grund, warum Sie eine OptionalString bekommen ist, dass (notifications as! NSDictionary).value(forKey: "title"):

extension NSDictionary { 
    /* Return the result of sending -objectForKey: to the receiver. 
    */ 
    open func value(forKey key: String) -> Any? 
} 

ein Kosten OptionalAny, die es wird ein Optional sein String für Ihren speziellen Fall.


So müssen Sie die OptionalString auszupacken, um einen String zu bekommen, gibt es viele Möglichkeiten, um auszupacken, aber die sicherste ist Optional Auswickeln.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell() 
    let notifications = self.newsModel.data[indexPath.row] 

    if let dictionary = notifications as? NSDictionary { 
    if let title = dictionary.value(forKey: "title") as? String { 
     cell.textLabel?.text = title 
    } 
    } 

    return cell 
} 

Wenn Sie Zeit haben, können Sie ein wenig über Optionals lesen, ich habe einen Beitrag über sie erstellt: https://medium.com/@wilson.balderrama/what-are-optionals-in-swift-3-b669ca4c2f12#.rb76h4r9k

Verwandte Themen