Swift 线程1 EXC错误
我一直在创建firebase数据库,但我有一个错误,我就是无法通过。它说: 线程1 exc_bad_指令(代码=exc_i386_invop子代码=0x0)' 这可能是一些简单的事情,我只是看看过去。有什么想法吗 这是代码&错误在代码行上:Swift 线程1 EXC错误,swift,uitableview,Swift,Uitableview,我一直在创建firebase数据库,但我有一个错误,我就是无法通过。它说: 线程1 exc_bad_指令(代码=exc_i386_invop子代码=0x0)' 这可能是一些简单的事情,我只是看看过去。有什么想法吗 这是代码&错误在代码行上: // // Database.swift // intern // // Created by Lani Daniels on 8/20/17. // Copyright © 2017 Lani Daniels. All rights reser
//
// Database.swift
// intern
//
// Created by Lani Daniels on 8/20/17.
// Copyright © 2017 Lani Daniels. All rights reserved.
//
import UIKit
import Firebase
import FirebaseDatabase
struct PostStruct {
let title: String
let message: String
}
class DatabaseViewController: UITableViewController {
var posts: [PostStruct] = []
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let label1 = cell.viewWithTag(1)as! UILabel
label1.text=posts[indexPath.row].title
//下面的错误:线程1 exc\U bad\U指令(代码=exc\U i386\U invop子代码=0x0)
让label2=cell.viewWithTag(2)为!UILabel
label2.text=posts[indexPath.row].message
return cell
}
}
对于第一个错误:
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
将tableview更改为tableview
关于第二个问题:
似乎您正在尝试访问可能不是UILabel的插座。通过制作可选投标书来检查这一点,如:
let label2 = cell.viewWithTag(2) as? UILabel
并使用断点检查该标签。可能是零
另外,最好使用带有两个标签的自定义单元格类作为输出,然后在cellForRowAt方法中将该自定义单元格出列:
class CustomCell: UITableViewCell {
@IBOutlet weak var firstLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!
}
然后
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? CustomCell
cell?.firstLabel.text=posts[indexPath.row].title
cell?.secondLabel.text=posts[indexPath.row].message
return cell!
}
别忘了在interface builder中为单元格设置自定义类我没有投反对票,但这个问题应该以打字错误结束。因此,我更正了语法错误,但在下面的一行中出现了错误。上面写着“thread1 exc_bad_指令(code=exc_i386_invop subcode=0x0)”,你知道这是怎么回事吗?看起来你试图访问的出口可能不是UILabel。通过制作可选的biding来检查这一点,比如:让label2=cell.viewWithTag(2)作为?UILabel并使用断点检查该标签。可能是零。另外,最好使用一个带有两个标签的自定义单元格类作为输出,然后将cellForRowAt methodSwift中的自定义单元格出列,因为它区分大小写。因此,我更正了语法错误,但在下面的一行中出现了错误。它说“线程1 exc\U bad\U指令(代码=exc\U i386\U invop子代码=0x0)”知道这是怎么回事吗?它不能在两行上都崩溃。。。告诉我们它在哪条线路上崩溃?