Quick parsing of CSV file from API is not shared with delimiter

I am trying to pass data to tableView cells. Networking works because the list of items is displayed in the first cell.

For example, the list looks like this: sensor1, sensor2, sensor3, .... but it should be like:

sensor1

sensor2

...

this is how i parse a csv file

struct ParseCVS { func parseURL (contentsOfURL: NSURL, encoding: NSStringEncoding) -> ([String])?{ let rowDelimiter = "," var nameOfSensors:[String]? do { let content = try String(contentsOfURL: contentsOfURL, encoding: encoding) print(content) nameOfSensors = [] let columns:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String] for column in columns { let values = column.componentsSeparatedByString(rowDelimiter) if let nameOfSensor = values.first { nameOfSensors?.append(nameOfSensor) } } } catch { print(error) } return nameOfSensors } } 

and this is my TableViewController

 class TableViewController: UITableViewController { // Array which will store my Data var nameOfSensorsList = [String]() override func viewDidLoad() { super.viewDidLoad() guard let wetterURL = NSURL(string: "http://wetter.htw-berlin.de/phpFunctions/holeAktuelleMesswerte.php?mode=csv&data=1") else { return } let parseCSV = ParseCVS() nameOfSensorsList = parseCSV.parseURL(wetterURL, encoding: NSUTF8StringEncoding)! tableView.estimatedRowHeight = 100.0 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return nameOfSensorsList.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MenuTableViewCell cell.nameLabel?.text = nameOfSensorsList[indexPath.row] return cell } } 

If anyone has any ideas, I would really appreciate it.

0
ios swift csv
source share
1 answer

You forgot to iterate over an array of values. Try something like this:

 for column in columns { let values = column.componentsSeparatedByString(rowDelimiter) print(values.count) for value in values { nameOfSensors?.append(value) } } 
0
source share

All Articles