I just started using Swift
and I would like to display the result of parsing a JSON
file in a UITableView
. The problem comes from the initialization of my NSMutableArray
news which will contain all the objects to display.
I have the Error :
error : "/Users/******/Documents/developper/******/*******
/NewsTableViewController.swift:79:22: 'NSMutableArray?'
does not have a member named 'count'
Code is :
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
}
Create a mutable array at the top of your class, and don't put any "?" or "!" after news.
var news: NSMutableArray = []
You also need to call reloadData on your table view at the end of the connectionDidFinishLoading method
Putting ?
character at the end of your class while declaring a variable creates an Optional
variable. Optional
variables can be nil or have a value Optional(value). You have to unwrap the Optional to use it.
var news:NSMutableArray?
creates an optional
array.
items?.count
will also return an optional Optional(Int). This cannot be returned in the UITableView
count method.
I don't think you need to use an optional here. Instead you can use an empty array for convenience.
var news: NSMutableArray = []
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.