swing1234
swing1234

Reputation: 233

iOS/Xcode: Koloda framework: Unexpectedly found nil while implicitly unwrapping an Optional value

I am trying to implement this Koloda framework into my app. (https://github.com/Yalantis/Koloda). However, when I run my app, I get an error in the line kolodaView.delegate = self which says "Unexpectedly found nil while implicitly unwrapping an Optional value"

I have been trying to debug the code for hours but could not understand where a nil value is coming from. Here is my view controller file below:

import UIKit
import Koloda
import pop

private let numberOfCards: Int = 5
private let frameAnimationSpringBounciness: CGFloat = 9
private let frameAnimationSpringSpeed: CGFloat = 16
private let kolodaCountOfVisibleCards = 2
private let kolodaAlphaValueSemiTransparent: CGFloat = 0.1

class CardViewController: UIViewController {
    
    @IBOutlet weak var kolodaView: CardView!
    //MARK: Lifecycle
    
    fileprivate var dataSource: [UIImage] = {
        var array: [UIImage] = []
        for index in 0..<numberOfCards {
            array.append(UIImage(named: "cards_\(index + 1)")!)
        }
        return array
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        kolodaView.alphaValueSemiTransparent = kolodaAlphaValueSemiTransparent
//        kolodaView.countOfVisibleCards = kolodaCountOfVisibleCards
        kolodaView.delegate = self
        kolodaView.dataSource = self
//        kolodaView.animator = BackgroundKolodaAnimator(koloda: kolodaView)
//        self.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
    }
    
    
//    //MARK: IBActions
//    @IBAction func leftButtonTapped() {
//        kolodaView?.swipe(.left)
//    }
//
//    @IBAction func rightButtonTapped() {
//        kolodaView?.swipe(.right)
//    }
//
//    @IBAction func undoButtonTapped() {
//        kolodaView?.revertAction()
//    }
}

//MARK: KolodaViewDelegate
extension CardViewController: KolodaViewDelegate {
    
    func kolodaDidRunOutOfCards(_ koloda: KolodaView) {
        kolodaView.resetCurrentCardIndex()
        kolodaView.reloadData()
    }
    
    func koloda(_ koloda: KolodaView, didSelectCardAt index: Int) {
        let myUrl = "https://yalantis.com/"
        if let url = URL(string: "\(myUrl)"), !url.absoluteString.isEmpty {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
    
    func kolodaShouldApplyAppearAnimation(_ koloda: KolodaView) -> Bool {
        return true
    }

    func kolodaShouldMoveBackgroundCard(_ koloda: KolodaView) -> Bool {
        return false
    }

    func kolodaShouldTransparentizeNextCard(_ koloda: KolodaView) -> Bool {
        return true
    }

    func koloda(kolodaBackgroundCardAnimation koloda: KolodaView) -> POPPropertyAnimation? {
        let animation = POPSpringAnimation(propertyNamed: kPOPViewFrame)
        animation?.springBounciness = frameAnimationSpringBounciness
        animation?.springSpeed = frameAnimationSpringSpeed
        return animation
    }
}

// MARK: KolodaViewDataSource
extension CardViewController: KolodaViewDataSource {
    
    func kolodaSpeedThatCardShouldDrag(_ koloda: KolodaView) -> DragSpeed {
        return .default
    }
    
    func kolodaNumberOfCards(_ koloda: KolodaView) -> Int {
        return numberOfCards
    }
    
    func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
        return UIImageView(image: UIImage(named: "cards_\(index + 1)"))
    }
    
//    func koloda(_ koloda: KolodaView, viewForCardOverlayAt index: Int) -> OverlayView? {
//        return Bundle.main.loadNibNamed("CustomOverlayView", owner: self, options: nil)?[0] as? OverlayView
//    }
 }

If anyone could point out how self.delegate is returning nil, that would be appreciated.

Upvotes: -1

Views: 256

Answers (1)

Frankenstein
Frankenstein

Reputation: 16351

The error states clearly that something is nil and you can't force unwrap in this line:

kolodaView.delegate = self

Here, obviously only kolodaView can produce the crash. It means in the following line the connection to the storyboard was lost. Reconnect to the kolodaView in the storyboard in the following line.

@IBOutlet weak var kolodaView: CardView!

Upvotes: 0

Related Questions