2016-11-07 3 views
-1

Ich versuche, Daten von einer URL in Swift 3 mit Alamofire zu erhalten. In der Zelle werden keine Daten übertragen. Ich weiß, dass ich irgendwo etwas vermasselt habe, aber ich bin ziemlich neu zu swift 3 und kann das Problem nicht sehen, der Code ist unten:Es kommen keine Daten von JSON mit Alamofire Swift 3 und Xcode 8 beta

JSON Struktur.

{ 
     posts: 
 [ 
     
 { 
     id: “000000”, 
     url: "/content/interview", 
     date: "2016-11-03 09:01:41", 
     modified: "2016-11-03 09:03:47", 
     title: "An interview", 
     summary: 
 { 
     value: "<p>Lorem ipsum is simply dummy text</p> ", 
     format: "filtered_html" 
     } 
    ] 
} 

Swift:

import UIKit 
import Alamofire 


class TableViewController: UITableViewController { 


var titles = [String]() 

var mainURL = "https://www.testapi.com" 

typealias JSONstandard = [String : AnyObject] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    callAlamo(url: mainURL) 
} 

func callAlamo(url : String){ 
    Alamofire.request(url).responseJSON(completionHandler: { 
     response in 

     self.parseData(JSONData: response.data!) 


    }) 

} 

func parseData(JSONData : Data) { 
    do { 
     var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard 

     print(readableJSON) 
     if let post = readableJSON["posts"] as? JSONstandard { 
      if let posts = post["posts"] { 
       for i in 0..<posts.count { 
        let post = posts[i] as! JSONstandard 

        let title = post["title"] as! String 
        titles.append(title) 

        self.tableView.reloadData() 

       } 
      } 

     } 

    } 


    catch { 
     print(error) 
    } 


} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return titles.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

    cell?.textLabel?.text = titles[indexPath.row] 

    return cell! 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 

Die oben bringen keine Daten in die "Zelle". Jede Hilfe wäre willkommen.

Antwort

0

allererst ein JSON-Wörterbuch in Swift 3 [String:Any]

typealias JSONstandard = [String : Any] 

Der Autor des Tutorials nennen sollte es JSONdictionary, um es klarer zu machen.

Der Wert für posts ist ein Array, JSON Sammlung Objekte sehr leicht zu identifizieren:

  • {} ist Wörterbuch

  • ... 
        if let posts = readableJSON["posts"] as? [JSONstandard] { 
        for post in posts { // don't use ugly C-style loops 
         let title = post["title"] as! String 
         titles.append(title) 
        } 
        self.tableView.reloadData() // reload the table view after the loop 
        } 
        ... 
    

    PS

    • [] ist Array: In Swift (1-3) passiert .mutableContainers ist ziemlich nutzlos.

    +0

    Danke, das hat super funktioniert! Ich denke, dass das Einbringen anderer Daten das gleiche Prinzip wäre. – rob

    0

    Sie Problem hier ist,

     if let posts = post["posts"] { 
           --- 
          } 
    

    in vorherigen Zeile sein können Sie Beiträge readableJSON [ "Beiträge"] erhalten. Post hat keinen Knoten namens "posts".

    versuchen diesen Code:

     if let allPost = readableJSON["posts"] { 
          var posts = allPost as! NSArray 
           for post in posts { 
            let title = post["title"] as! String 
            titles.append(title) 
            self.tableView.reloadData() 
           } 
          } 
    
    +0

    Bitte schlagen Sie nie 'NSArray' in Swift in solch einem Fall vor – vadian

    +0

    ok, danke @vadian –

    0

    hinzufügen UITableViewDataSource Methode:

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
        return 1 
    } 
    

    Nicht reloadData() zu oft:

    func parseData(JSONData : Data) { 
        do { 
         var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard 
    
         print(readableJSON) 
         if let post = readableJSON["posts"] as? JSONstandard { 
          if let posts = post["posts"] { 
           for i in 0..<posts.count { 
            let post = posts[i] as! JSONstandard 
            let title = post["title"] as! String 
            titles.append(title)    
           } 
           self.tableView.reloadData() // moved out of loop 
          } 
         } 
        } 
        catch { 
         print(error) 
        } 
    } 
    

    Wenn das Problem weiterhin besteht:

    • Log: titles.count
    +0

    Das Problem wird bestehen bleiben – vadian

    Verwandte Themen