2016-11-07 60 views
-1

我想從Alamofire的Swift 3中的url中獲取數據。單元格上沒有數據。我知道我在某處搞亂了某些東西,但我對swift 3相當陌生,無法看到問題,代碼如下:沒有數據從分析JSON與Alamofire Swift 3和Xcode 8測試版進入

JSON結構。

{ 
     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" 
     } 
    ] 
} 

夫特:

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. 
} 


} 

上面帶來不數據變成 「小區」。任何幫助,將不勝感激。

回答

0

斯威夫特3全部JSON字典首先是[String:Any]

typealias JSONstandard = [String : Any] 

本教程的作者應該命名爲JSONdictionary,以使其更清晰。

posts的值是數組,JSON集合對象是很容易識別:

  • []是陣列
  • {}是字典

... 
    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:在斯威夫特(1-3)傳遞.mutableContainers是相當無用的。

+0

謝謝,這很好!我想引入其他數據幾乎是一樣的原理。 – rob

0

您的問題是這裏可能是

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

中前行您readableJSON [ 「上崗」]得到的帖子。 Post沒有名爲「posts」的節點。

試試這個代碼:

 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

請永遠不要在Swift中提示'NSArray'在這種情況下 – vadian

+0

好的,謝謝@vadian –

0

添加UITableViewDataSource梅索德:

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

不要reloadData()過於頻繁:

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) 
    } 
} 

如果問題仍然存在:

  • 日誌:titles.count
+0

問題會持續存在 – vadian

相關問題