Created
April 6, 2015 12:30
-
-
Save Jan0707/b401441ee65c7a26f485 to your computer and use it in GitHub Desktop.
Answer to question on SO http://stackoverflow.com/questions/29461926/how-to-transfer-data-from-nsarray-to-string-in-swift/29469719
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
// | |
// MasterViewController.swift | |
// HackerNewsTitles | |
// | |
// Created by Jan Gregor Triebel on 06.04.15. | |
// Copyright (c) 2015 Jan Gregor Triebel. All rights reserved. | |
// | |
import UIKit | |
class MasterViewController: UITableViewController { | |
var titles = [Int]() | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
refresh() | |
} | |
func refresh() { | |
let baseURL = "https://hacker-news.firebaseio.com/v0/topstories.json" | |
if let url = NSURL(string: baseURL) { | |
let taskURL = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: | |
{ (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in | |
if error != nil { | |
println("NSURL-Error: \(error.localizedDescription)") | |
} else { | |
var jsonError: NSError? | |
if let topStories = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as? [Int] { | |
self.titles = topStories | |
self.tableView.reloadData() | |
} else { | |
println("JSON-Error: \(jsonError)") | |
} | |
} | |
}) | |
taskURL.resume() | |
} | |
} | |
// MARK: - Table View | |
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return 1 | |
} | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return titles.count | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment