简体   繁体   English

在解包Optional值时,Swift Delegate意外返回nil

[英]Swift Delegate returns unexpectedly found nil while unwrapping an Optional value

Setting up my delegate within the protocol continues to throw nil. 在协议中设置我的委托继续抛出零。 I have attempted numerous approaches suggested in other posts. 我尝试过其他帖子中提出的众多方法。 I am attempting to get my protocol + delegate up and running, however, unable to solve why it continues to throw nil. 我试图让我的协议+委托启动并运行,然而,无法解决为什么它继续抛出零。

Force unwrapping, calling the delegate from various locations in the file, removing & adding Weak Var. 强制解包,从文件中的各个位置调用委托,删除并添加Weak Var。

List View: 列表显示:

protocol MixPlayer : class {
    func playMix(message: String)
}


class IssueViewController: UIViewController {

    @IBOutlet weak var issueCollection: UICollectionView!
    @IBOutlet weak var issueImage: UIImageView!

    var viewController: ViewController?

    var collectionViewtitle: String?
    var mixImageName: String?

    var mixList: [[String: String]]!

    weak var mixDelegate: MixPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        issueCollection.dataSource = self
        issueCollection.delegate = self

    }

}
....

extension IssueViewController: UICollectionViewDelegate, UICollectionViewDataSource {

....

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        if let url = mixList[indexPath.row]["url"] {
            mixDelegate?.playMix(message: url)
        }
    }   
}

View Controller: 查看控制器:

import UIKit

class ViewController: UIViewController  {
    @IBOutlet weak var mainContainer: UIView!
    @IBOutlet weak var playerEmbedView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let secondVC = IssueViewController()
        secondVC.mixDelegate = self

    }
}

extension ViewController: MixPlayer {
    func playMix(message: String) {
        print(message)
    }
}

Any attempt at calling 任何打电话的尝试

mixDelegate?.playMix(message: url)

Is unsuccessful. 不成功。 Currently I'm just trying to log basic print statements. 目前我只是想记录基本的打印语句。

Probably your secondVC inside the ViewController gets deallocated. 可能你的ViewControllersecondVC被解除分配。 Make that a property in your ViewController . ViewController一个属性。

class ViewController: UIViewController  {
    @IBOutlet weak var mainContainer: UIView!
    @IBOutlet weak var playerEmbedView: UIView!

    lazy var secondVC: IssueViewController = {
        let secondVC = IssueViewController()
        secondVC.mixDelegate = self
        return secondVC
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    ... 
}

To know if your class gets deallocated, add a deinit with a print statement on it. 要知道您的类是否已取消分配,请在其上添加带有print语句的deinit。

deinit {
    print("Deallocated")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:解开可选值时意外发现nil-委托 - fatal error: unexpectedly found nil while unwrapping an Optional value - delegate Swift - 在展开Optional值时意外地发现nil - 从委托调用变量 - Swift - unexpectedly found nil while unwrapping an Optional value - calling variable from delegate Swift:在为UITextField对象设置委托时展开一个可选值时意外发现nil - Swift: unexpectedly found nil while unwrapping an Optional value when setting delegate for UITextField object RXSwift 绑定在展开可选值时返回意外发现 nil - RXSwift Binding returns Unexpectedly found nil while unwrapping an Optional value 迅捷的Xcode:解开可选值(lldb)时意外发现nil - swift Xcode: unexpectedly found nil while unwrapping an Optional value (lldb) swift imageView,collectionView在展开Optional值时意外找到nil - swift imageView, collectionView, unexpectedly found nil while unwrapping an Optional value Swift:在展开可选值NSObject时意外发现nil - Swift: unexpectedly found nil while unwrapping an Optional value, NSObject 在swift解开Optional值时意外发现nil - unexpectedly found nil while unwrapping an Optional value in swift Swift NSUserDefaults 在解开可选值时意外发现 nil - Swift NSUserDefaults unexpectedly found nil while unwrapping an Optional value Swift:致命错误:在展开可选值时意外发现nil - Swift : fatal error: unexpectedly found nil while unwrapping an Optional value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM