Ios JSON表格视图Swift错误
我正在尝试用这个JSON URL填充tableview。我验证了URL,它工作正常。我的代码有问题。我有一个“Repository.swift”文件,里面有一个类。这是我的代码:我无法解决问题Ios JSON表格视图Swift错误,ios,json,swift,tableview,Ios,Json,Swift,Tableview,我正在尝试用这个JSON URL填充tableview。我验证了URL,它工作正常。我的代码有问题。我有一个“Repository.swift”文件,里面有一个类。这是我的代码:我无法解决问题 var repositories = [Repository]() override func viewDidLoad() { super.viewDidLoad() let reposURL = NSURL(string: "https://www.kimonolabs.com/ap
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-catch 我修正了你的密码
override func viewDidLoad() {
super.viewDidLoad()
let reposURL = NSURL(string: "https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y")!
if let JSONData = NSData(contentsOfURL: reposURL) {
do{
let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: .MutableContainers)
if let reposArray = json["items"] as? [NSDictionary] {
for item in reposArray {
// repositories.append(Repository(json: item))
}
}
}catch {
//do whatever
}
}
}
请试一试 调用可以抛出,但未标记为“try”,并且错误未被处理3错误1)“let”声明无法计算属性2)无法为NSDictionary类型的值下标?3) “是”测试总是正确的。。感谢您的努力:)3个错误1)“let”声明无法计算属性2)无法为NSDictionary类型的值下标?3) “是”测试总是正确的。。谢谢你的努力。。那一点现在看起来没问题了。。。但这一行导致了另一个错误:“定义与以前的值冲突”:override func tableView(tableView:UITableView,cellForRowAtIndexPath-indexath:nsindexath)->UITableViewCell{let cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forindexath:indexath)@DonDraper这是因为您的代码没有正确的右括号
}
,请尝试修复该问题,如果您愿意,请将整个viewcontroller放入其中,我尝试帮助修复括号:不可变可能适用于此行:“for item in reposArray{”@DonDraper,这是因为您没有在for中使用变量项
。请使用该变量,或将其替换为\uu