iOS/Xcode:Koloda 框架:在隐式展开 Optional 值时意外发现 nil
Posted
技术标签:
【中文标题】iOS/Xcode:Koloda 框架:在隐式展开 Optional 值时意外发现 nil【英文标题】:iOS/Xcode: Koloda framework: Unexpectedly found nil while implicitly unwrapping an Optional value 【发布时间】:2020-06-29 20:30:49 【问题描述】:我正在尝试将这个 Koloda 框架实现到我的应用程序中。 (https://github.com/Yalantis/Koloda)。但是,当我运行我的应用程序时,我在 kolodaView.delegate = self
行中收到一个错误,上面写着“Unexpectedly found nil while implicitly unwrapping an Optional value”
我已经尝试调试代码几个小时,但无法理解 nil 值的来源。下面是我的视图控制器文件:
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
//
如果有人能指出 self.delegate
如何返回 nil,将不胜感激。
【问题讨论】:
您是否确保“卡片视图”指向故事板中的正确委托? 是的,卡片视图是一个返回 CGrect 并在情节提要中创建 UIView 的类。我在 Assets 文件夹中有图片“card_1, card_2, ..”,理想情况下 main.storyboard 中的 UIView 应该显示这些卡片并允许用户在它们上向左或向右滑动。 【参考方案1】:错误清楚地表明某些东西是 nil 并且您不能在这一行中强制展开:
kolodaView.delegate = self
这里,显然只有kolodaView
可以产生崩溃。这意味着在以下行中与情节提要的连接丢失了。在以下行中重新连接到情节提要中的kolodaView
。
@IBOutlet weak var kolodaView: CardView!
【讨论】:
以上是关于iOS/Xcode:Koloda 框架:在隐式展开 Optional 值时意外发现 nil的主要内容,如果未能解决你的问题,请参考以下文章
在隐式展开可选值 AVAUDIO Player SWIFT 时意外发现 nil
UITableViewCell 中的 AVPlayer 导致“在隐式展开可选值时意外发现 nil”错误
Swift Xcode 致命错误:在隐式展开可选值时意外发现 nil [重复]
想要将 Slidervalue 设置为 UserDefault:线程 1:致命错误:在隐式展开可选值时意外发现 nil [重复]