Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 意外地找到nil,但打印显示有效字符串_Swift - Fatal编程技术网

Swift 意外地找到nil,但打印显示有效字符串

Swift 意外地找到nil,但打印显示有效字符串,swift,Swift,我试图设置一个图像和两个标签文本,如下所示: cell.ogImageView.sd_setImage(with: URL(string: post.ogImageURL)) cell.titleLabel.text = post.title cell.urlLabel.text = post.url 如果在此之前打印字符串: print(post.ogImageURL) print(post.title) print(post.url) 我得到一个有效的字符串,以及错误: 注释掉sd\u

我试图设置一个图像和两个标签文本,如下所示:

cell.ogImageView.sd_setImage(with: URL(string: post.ogImageURL))
cell.titleLabel.text = post.title
cell.urlLabel.text = post.url
如果在此之前打印字符串:

print(post.ogImageURL)
print(post.title)
print(post.url)
我得到一个有效的字符串,以及错误:

注释掉
sd\u setImage…
也会给
标题和
url带来相同的错误

问题:这里发生了什么?既然字符串很好,我猜
URL
就是这里返回nil的那个?但为什么我的标题和url也会出错,尽管它们都很好

单元格的类:

class LinksCell: MGSwipeTableCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var urlLabel: UILabel!
    @IBOutlet weak var ogImageView: UIImageView!
}
我在其中填充它们的
viewDidLoad()

override func viewDidLoad() {
        super.viewDidLoad()
linksTableView.register(LinksCell.self, forCellReuseIdentifier: "linksCellident")

        dataSource = FUITableViewDataSource.init(query: getQuery()) { (tableView, indexPath, snap) -> MGSwipeTableCell in
            let cell = tableView.dequeueReusableCell(withIdentifier: "linksCellident", for: indexPath) as! LinksCell

            guard let post = LinksPost.init(snapshot: snap) else { return cell }
            print(post.ogImageURL)
            print(post.title)
            print(post.url)
            cell.ogImageView.sd_setImage(with: URL(string: post.ogImageURL))
            cell.titleLabel.text = post.title
            cell.urlLabel.text = post.url

            return cell
    }
    dataSource?.bind(to: linksTableView)
    linksTableView.delegate = self
}
“LinksPost”类:

调试变量也会显示有效字符串:

调试
单元格的内容

如果删除此行,一切似乎都正常:

linksTableView.register(...)

我对iOS开发非常不熟悉,所以对此持保留态度。(也许专家可以插话?)我认为问题是,这已经通过你的故事板中的原型内容设置好了,再次在这里注册基本上取消了设置的出口。您的
ogImageView
titleLabel
、和
urlabel
的值都是
nil
我不是100%确定,但是您的tableView.dataSource链接正确吗?你的手机是零吗?在FUITableViewDataSource回调的每个步骤后尝试断点,看看是哪一行导致了崩溃。

这个URL上似乎什么都没有,只是尝试将URL放入浏览器中,尽管如此。注释掉图像部分还会为两个标签文本提供
nil
。如果所有字符串都有效,则其中一个(或两个)标签可能未正确连接。设置断点并查找。您使用的是静态单元格还是动态单元格?如果是静态的,请删除您的插座并重置它们。您在哪一行收到此错误?非常感谢您的帮助!伟大的洞察力。似乎在那里使用原型单元时,需要以不同的方式实现此注册。@ffritz假设这已经解决了,您能接受这个答案吗?这样其他人就不会继续尝试了?:-)我会尽快做的。还需要验证一件事。这不是问题的真正答案,而且问题已经得到了回答。
linksTableView.register(...)