Created
December 17, 2016 00:51
-
-
Save knowsuchagency/aa0974d7487bf737627d67059fdf9ae9 to your computer and use it in GitHub Desktop.
code for initial view controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// VotesTableViewController.swift | |
// Voting | |
// | |
// Created by Stephan Fitzpatrick on 12/16/16. | |
// Copyright © 2016 Stephan Fitzpatrick. All rights reserved. | |
// | |
import UIKit | |
class VotesTableViewController: UITableViewController { | |
// set a variables for the votes | |
var votes: [Vote] = [Vote]() | |
// this is the event url constant | |
let eventURL = api.eventURLFor(id: 1) | |
//@IBOutlet weak var tableView: UITableView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.title = "Votes" | |
let url = URL(string: eventURL) | |
let task = URLSession.shared.dataTask(with: url!) { data, response, error in | |
guard error == nil else { | |
print(error as Any) | |
return | |
} | |
guard let data = data else { | |
print("Data is empty") | |
return | |
} | |
// update votes | |
self.votes = self.votesFromData(data: data) | |
// update UI | |
DispatchQueue.main.async { | |
self.tableView.reloadData() | |
} | |
} | |
task.resume() | |
} | |
// MARK: - Table view data source | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.votes.count | |
} | |
func votesFromData(data: Data?) -> [Vote] { | |
// No data, return an empty array | |
guard let data = data else { | |
return [] | |
} | |
// Parse the Data into a JSON Object | |
let JSONObject = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions(rawValue: 0)) | |
// Insist that this object must be a dictionary | |
guard let JSONDictionary = JSONObject as? [String : Any] else { | |
assertionFailure("Failed to parse data. data.length: \(data.count)") | |
return [Vote]() | |
} | |
// Print the object, for now, so we can take a look | |
//print(JSONDictionary) | |
// Pretty Print the string, for debugging | |
let prettyData = try! JSONSerialization.data(withJSONObject: JSONObject, options: .prettyPrinted) | |
let prettyString = String(data: prettyData, encoding: String.Encoding.utf8) | |
print(prettyString!) | |
// convert votes into an array of dictionaries | |
let voteDictionaries = JSONDictionary["votes"] as! [[String : Any]] | |
let votes = voteDictionaries.map { | |
Vote(dictionary: $0)! | |
} | |
return votes | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) | |
let vote = self.votes[indexPath.row] | |
cell.textLabel!.text = "\(vote.name) -> \(vote.count)" | |
return cell | |
} | |
/* | |
// Override to support conditional editing of the table view. | |
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { | |
// Return false if you do not want the specified item to be editable. | |
return true | |
} | |
*/ | |
/* | |
// Override to support editing the table view. | |
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { | |
if editingStyle == .delete { | |
// Delete the row from the data source | |
tableView.deleteRows(at: [indexPath], with: .fade) | |
} else if editingStyle == .insert { | |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view | |
} | |
} | |
*/ | |
/* | |
// Override to support rearranging the table view. | |
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { | |
} | |
*/ | |
/* | |
// Override to support conditional rearranging of the table view. | |
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { | |
// Return false if you do not want the item to be re-orderable. | |
return true | |
} | |
*/ | |
/* | |
// MARK: - Navigation | |
// In a storyboard-based application, you will often want to do a little preparation before navigation | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
// Get the new view controller using segue.destinationViewController. | |
// Pass the selected object to the new view controller. | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment