By rulilg

2014-07-09 00:12:34 8 Comments

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?

Screenshot for warning on Console


@greeninaqua 2014-07-27 13:58:14

I don't know is it bug or something but your labels and other UIs does not initialized automatically when you use custom cell. You should try this in your UICollectionViewController class. It worked for me.

override func viewDidLoad() {

    let nib = UINib(nibName: "<WhateverYourNibName>", bundle: nil)
    self.collectionView.registerNib(nib, forCellReuseIdentifier: "title")

@rj2700 2016-01-20 00:53:55

How do you find >WhateverYourNibName> is?

@Lawliet 2017-02-08 04:16:27

It should be the name of your custom collectionView cell (.xib)

@David DelMonte 2016-05-25 10:06:07

Same message here, but with a different cause. I had a UIBarButton that pushed a segue. The segue lacked an identifier.

@ekeith 2016-05-22 17:23:22

I had the same problem on Xcode 7.3. My solution was to make sure my cells had the correct reuse identifiers. Under the table view section set the table view cell to the corresponding identifier name you are using in the program, make sure they match.

@Hugo Sequeira 2016-01-05 12:33:22

I was having the same issue and the reason for it was because I was trying to load a UIImage with the wrong name. Double-check .setImage(UIImage(named: "-name-" calls and make sure the name is correct.

@Kishore Kumar 2015-12-28 10:45:55

fatal error: unexpectedly found nil while unwrapping an Optional value

  1. Check the IBOutlet collection , because this error will have chance to unconnected uielement object usage.

:) hopes it will help for some struggled people .

@Pruthvid 2015-03-17 19:01:14

I got his error when i was trying to access UILabel. I forgot to connect the UILabel to IBOutlet in Storyboard and that was causing the app to crash with this error!!

@iOS Developer 2015-09-22 21:38:07

Replace this line cell.labelTitle.text = "This is a title"
with cell.labelTitle?.text = "This is a title"

@Abhijeet 2015-08-28 10:20:34

Nil Coalescing Operator can be used as well.

rowName = rowName != nil ?rowName!.stringFromCamelCase():""

@Connor 2014-07-09 00:28:01

You can prevent the crash from happening by safely unwrapping cell.labelTitle with an if let statement.

if let label = cell.labelTitle{
    label.text = "This is a title"

You will still have to do some debugging to see why you are getting a nil value there though.

@SwiftMatt 2016-01-16 10:29:48

This is weird to me, because isn't it only an issue if you getting the property rather than setting it?

@SwiftMatt 2016-01-16 10:32:46

Actually, my issue is different, tableView.tableFooterView = UIView() is where my code breaks

@zeeshan 2017-01-13 16:02:24

This is very important good practice for any production code.

@Ron Fessler 2014-10-10 19:50:19

Check if the cell is being registered with self.collectionView.registerClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String). If so, then remove that line of code.

See this answer for more info: Why is UICollectionViewCell's outlet nil?

"If you are using a storyboard you don't want to call this. It will overwrite what you have in your storyboard."

@Christer 2014-08-30 12:18:32

I searched around for a solution to this myself. only my problem was related to UITableViewCell Not UICollectionView as your mentioning here.

First off, im new to iOS development. like brand new, sitting here trying to get trough my first tutorial, so dont take my word for anything. (unless its working ;) )

I was getting a nil reference to cell.detailTextLabel.text - After rewatching the tutorial video i was following, it didnt look like i had missed anything. So i entered the header file for the UITableViewCell and found this.

var detailTextLabel: UILabel! { get } // default is nil. label will be created if necessary (and the current style supports a detail label).

So i noticed that it says (and the current style supports a detail label) - Well, Custom style does not have a detailLabel on there by default. so i just had to switch the style of the cell in the Storyboard, and all was fine.

Im guesssing your label should always be there?

So if your following connor`s advice, that basically means, IF that label is available, then use it. If your style is correctly setup and the reuse identifier matches the one set in the Storyboard you should not have to do this check unless your using more then one custom cell.

@Lukesivi 2015-12-23 17:11:58

I was having this issue with a PFQueryTableViewController. After reading this answer I had a lightbulb moment that the table in Parse may be empty (even though it's not but maybe for first use it returns nil as default?). But i changed from which column I was querying from and it works.

@Max MacLeod 2014-08-11 09:17:01

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

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.

That's determined by the exclamation mark after AnyObject:


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

The Swift Programming Language tells us:

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.

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

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

@Ran 2015-02-04 11:15:01

This is the correct answer, not the one that is marked.

@PostCodeism 2015-09-19 00:09:43

So basically an Implicitly Unwrapped Optional should never return nil

@Jignesh 2016-01-04 20:14:02

@Ran This is the exact reason, we should always read the other answers as well.

@ClareHuxtable 2014-08-03 22:31:39

I was having this issue as well and the problem was in the view controllers. I'm not sure how your application is structured, so I'll just explain what I found. I'm pretty new to xcode and coding, so bear with me.

I was trying to programmatically change the text on a UILabel. Using "self.mylabel.text" worked fine until I added another view controller under the same class that didn't also include that label. That is when I got the error that you're seeing. When I added the same UILabel and connected it into that additional view controller, the error went away.

In short, it seems that in Swift, all view controllers assigned to the same class have to reference the code you have in there, or else you get an error. If you have two different view controllers with different code, assign them to different classes. This worked for me and hopefully applies to what you're doing.

Related Questions

Sponsored Content

8 Answered Questions

9 Answered Questions

[SOLVED] How to detect tableView cell touched or clicked in swift

4 Answered Questions

[SOLVED] Swift Error - Use of undeclared type 'cell' - Collection View

2 Answered Questions

Adding UICollectionViewController to UIViewController Not Working

2 Answered Questions

[SOLVED] Implementing single cell selection to bring up a detail view in swift

1 Answered Questions

3 Answered Questions

unexpectedly found nil while unwrapping an Optional values access tableView Cell

  • 2015-11-03 09:12:11
  • Mohammad Alikhani
  • 213 View
  • 1 Score
  • 3 Answer
  • Tags:   ios swift tableview

1 Answered Questions

3 Answered Questions

[SOLVED] How can I optimize loading images from URL for uicollectionview?

Sponsored Content