致命错误:在解包可选值时意外发现nil

编程入门 行业动态 更新时间:2024-10-26 18:29:46
本文介绍了致命错误:在解包可选值时意外发现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

本文发布于:2023-11-26 14:15:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634105.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   意外   错误   发现   nil

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!