致命错误:在解包可选值时意外发现nil [英] Fatal error: unexpectedly found nil while unwrapping an Optional values

查看:913
本文介绍了致命错误:在解包可选值时意外发现nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift中使用了 UICollectionView ,但当我尝试更改单元格标签的文本时,我得到了。

I was using an UICollectionView in Swift but I get when I try to change the text of the cell's label.

    func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int
{
    return 5
}

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("title", forIndexPath: indexPath) as TitleCollectionViewCell
    // Next line: fatal error: unexpectedly found nil while unwrapping an Optional value
    cell.labelTitle.text = "This is a title"
    return cell
}



<有没有人知道这个?

Does anyone know about this?

推荐答案

几乎可以肯定,您的重用标识符title不正确。

Almost certainly, your reuse identifier "title" is incorrect.

我们可以从 UITableView.h dequeueReusableCellWithIdentifier 的方法签名,返回类型是 隐式解包可选

We can see from the UITableView.h method signature of dequeueReusableCellWithIdentifier that the return type is an Implicitly Unwrapped Optional:

func dequeueReusableCellWithIdentifier(identifier: String!) -> AnyObject! // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

这是由 AnyObject 之后的感叹号决定的:

That's determined by the exclamation mark after AnyObject:

    AnyObject!

所以,首先要考虑的是,什么是隐式解包可选?

So, first thing to consider is, what is an "Implicitly Unwrapped Optional"?

Swift编程语言告诉我们:


有时从程序的结构可以清楚地知道可选的
总是在第一次设置该值之后有一个值。在这些情况下,
对于每次访问时都不需要检查和解包可选的值
,因为可以安全地假设它始终具有
值。

Sometimes it is clear from a program’s structure that an optional will always have a value, after that value is first set. In these cases, it is useful to remove the need to check and unwrap the optional’s value every time it is accessed, because it can be safely assumed to have a value all of the time.

这些类型的期权被定义为隐式解包的
期权。你可以通过在
之后放置
感叹号(String!)而不是问号(String?)来编写一个隐式解包的可选项。你希望这个类型是可选的。

These kinds of optionals are defined as implicitly unwrapped optionals. You write an implicitly unwrapped optional by placing an exclamation mark (String!) rather than a question mark (String?) after the type that you want to make optional.

所以,基本上,某些东西可能在某一点上为零,但从某些角度来看,它永远不会再为零。因此,我们通过将其作为未包装的值来节省一些麻烦。

So, basically, something that might have been nil at one point, but which from some point on is never nil again. We therefore save ourselves some bother by taking it in as the unwrapped value.

在这种情况下, dequeueReusableCellWithIdentifier 返回这样的值。提供的标识符必须已经用于注册单元格以供重用。提供错误的标识符,dequeue找不到它,运行时返回一个永远不会发生的nil。这是一个致命错误,应用程序崩溃,控制台输出给出:

It makes sense in this case for dequeueReusableCellWithIdentifier to return such a value. The supplied identifier must have already been used to register the cell for reuse. Supply an incorrect identifier, the dequeue can't find it, and the runtime returns a nil that should never happen. It's a fatal error, the app crashes, and the Console output gives:

fatal error: unexpectedly found nil while unwrapping an Optional value

底线:检查.storyboard,Xib中指定的单元格重用标识符,或在代码中,并确保在出列时它是正确的。

Bottom line: check your cell reuse identifier specified in the .storyboard, Xib, or in code, and ensure that it is correct when dequeuing.

这篇关于致命错误:在解包可选值时意外发现nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆