【发布时间】:2015-10-23 00:14:39
【问题描述】:
我正在尝试使用此 JSON URL 填充 tableview。我验证了 URL,它工作正常。我的代码有问题。我有一个“Repository.swift”文件,里面有一个类。这是我的代码:我无法解决问题。
var repositories = [Repository]()
override func viewDidLoad() {
super.viewDidLoad()
let reposURL = NSURL(string: "https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y")!
if let JSONData = NSData(contentsOfURL: reposURL) {
if let json = NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
if let reposArray = json["items"] as? [NSDictionary] {
for item in reposArray {
repositories.append(Repository(json: item))
}
}
}
}
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.repositories.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = repositories[indexPath.row].Event
cell.detailTextLabel?.text = repositories[indexPath.row].Hasta
return cell
}
【问题讨论】:
-
调用可以抛出但没有标记'try'并且错误未处理
-
3 个错误 1)'let' 声明不能是计算属性 2) 不能下标 NSDictionary 类型的值? 3)“是”测试总是正确的..感谢您的努力:)