簡體   English   中英

在Swift中為json文件初始化字典數組

[英]initialize array of dictionaries for a json file in Swift

我剛剛開始使用Swift ,我想在UITableView顯示解析JSON文件的結果。 問題來自我的NSMutableArray新聞的初始化,該新聞將包含要顯示的所有對象。

我有錯誤:

error : "/Users/******/Documents/developper/******/*******
/NewsTableViewController.swift:79:22: 'NSMutableArray?' 
does not have a member named 'count'

代碼是:

import UIKit

class NewsTableViewController: UITableViewController {

var bytes: NSMutableData?

// var news: NSArray = []

var news: NSMutableArray?

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()



        // this is our remote end point (similar to URLRequest in AS3)
    let request = NSURLRequest(URL: NSURL(string: "http://mangerdulion.com/wp-content/newsInnoven.json")!)

    // this is what creates the connection and dispatches the varios events to track progression, etc.
    let loader = NSURLConnection(request: request, delegate: self, startImmediately: true)

}
func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
    self.bytes?.appendData(conData)
}
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    self.bytes = NSMutableData()
}

func connectionDidFinishLoading(connection: NSURLConnection!) {

    // we serialize our bytes back to the original JSON structure
    let jsonResult: Dictionary = NSJSONSerialization.JSONObjectWithData(self.bytes!, options: NSJSONReadingOptions.MutableContainers, error: nil) as Dictionary<String, AnyObject>

    // we grab the colorsArray element
    let results: NSArray = jsonResult["newsArray"] as NSArray

    self.news?.addObjectsFromArray(results)
    /*
    // we iterate over each element of the colorsArray array
    for item in results {
        // we convert each key to a String
        var titre: String = item["titreNews"] as String
        var date: String = item["dateNews"] as String
        var contenu: String = item["contenuNews"] as String
        println("\(titre): \(date):\(contenu)")

    }
 */

}

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

// MARK: - Table view data source



override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.

    //println(self.news?.count)

    return self.news.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell




    let titre: NSDictionary = self.news[indexPath.row] as NSDictionary

    cell.textLabel?.text = titre["titreNews"] as? String
    println(cell.textLabel?.text)


    // Get the formatted price string for display in the subtitle
    let contenu: NSString = titre["contenuNews"] as NSString

    cell.detailTextLabel?.text = contenu


    return cell
}

在類的頂部創建一個可變數組,不要添加任何“?” 要么 ”!” 新聞之后。

var news: NSMutableArray = []

您還需要在connectionDidFinishLoading方法的末尾在表視圖上調用reloadData

? 在類的末尾聲明一個變量時,該字符會創建一個Optional變量。 Optional變量可以為nil或具有值Optional(value)。 您必須打開Optional才能使用它。

var news:NSMutableArray? 創建一個optional數組。

items?.count也將返回可選的Optional(Int)。 無法在UITableView count方法中返回該值。

我認為您無需在此處使用可選選項。 相反,您可以使用一個空數組以方便使用。

var news: NSMutableArray = []

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM