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

查看:24
本文介绍了致命错误:在展开可选值时意外发现 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
}

有人知道吗?

推荐答案

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

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

dequeueReusableCellWithIdentifierUITableView.h方法签名可以看出,返回类型是一个Implicitly Unwrapped Optional:

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后面的感叹号决定:

    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.

这些类型的可选项被定义为隐式展开选项.您通过放置一个隐式展开的可选项感叹号(字符串!)而不是问号(字符串?)您要设为可选的类型.

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 返回这样的值是有意义的.提供的标识符必须已用于注册单元以供重用.提供不正确的标识符,出队找不到它,并且运行时返回一个永远不会发生的 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 或代码中指定的单元重用标识符,并确保在出列时它是正确的.

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

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