0

In my custom collectionview cell I have

@IBOutlet weak var boardNameLabel: UILabel!

var boardInfoDic: Dictionary? = [String : AnyObject]() 

func updateItemAtIndexPath(_ indexPath: NSIndexPath) {

    if let string = boardInfoDic?["description"]
        {
            boardNameLabel.text = String(format: "%@", string as! String)
        }
}

and i am sending data to boardInfoDic from collectionView cellForItemAt indexPath: as

let boardsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: KBoardsCollectionViewCellIdentifier, for: indexPath) as! BoardsCollectionViewCell
boardsCollectionViewCell.boardInfoDic = self.boardsDataArray?[indexPath.item] as Dictionary<String, AnyObject>?
boardsCollectionViewCell.updateItemAtIndexPath(indexPath as NSIndexPath)

but I am getting fatal error: unexpectedly found nil while unwrapping an Optional value, I was tried in multiple ways but no use. How can I fix this issue?

Outlet connection with UICollectionViewCell enter image description here

SriKanth
  • 459
  • 7
  • 27

4 Answers4

0

First confirm your boardInfoDic is not empty. Use this

    func updateItemAtIndexPath(_ indexPath: NSIndexPath) {

                print(boardInfoDic)
                boardNameLabel.text = String(self.boardInfoDic["description"]!)   

}
Chanchal Warde
  • 983
  • 5
  • 17
0

when you do if let string = boardInfoDic?["description"] that variable string is not of type String it is of type AnyObject. As a result, when you cast string to be a String, it is unable to make this type type cast, and, as a result, returns nil. in order to get a string from your dictionary, you need to access it with something of type AnyObject. For example

if let string = boardInfoDic?["description"]
        {
            boardNameLabel.text = String(format: "%@", string as! String)
        }  

Be sure to mark this as the answer if it helps you.

Gabe Spound
  • 146
  • 2
  • 14
0

Try optional conversion

if let string = boardInfoDic?["description"] as? String {
    boardNameLabel.text = String(format: "%@", string)
}
Vladimir Vlasov
  • 1,860
  • 3
  • 25
  • 38
0

This worked for me

if let string = boardInfoDic?["description"] as? String
    {
        boardNameLabel?.text = string
    }
SriKanth
  • 459
  • 7
  • 27