致命错误:在展开可选值时意外发现 nil

编程入门 行业动态 更新时间:2024-10-16 02:26:06
本文介绍了致命错误:在展开可选值时意外发现 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.

从dequeueReusableCellWithIdentifier的UITableView.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

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

发布评论

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

>www.elefans.com

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