setting delegate to self throwing error in Swift [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-13 04:22:43

问题


I am currently working on code that disables a UIImageView while an AVAudioPlayer is playing. The AVAudioPlayer and the ImageView I want to disable are located in two different classes. I tried writing a delegate to have my two classes communicate but I keep on getting the error "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" when trying to assign to self in my viewController.

The code for the class containing my AVAudioPlayer is below

import Foundation; import UIKit; import AVFoundation

protocol isOnProtocol{
func isOn()
}

class Card: NSObject
{
    var delegate: isOnProtocol?
    var player: AVAudioPlayer?
    var image: UIImage
    var soundUrl: String

    init(image: UIImage, soundUrl: String, isActive:Bool = true) {
        self.image = image
        self.soundUrl = soundUrl

    }

    func playSound()
    {
        guard let url = Bundle.main.url(forResource: self.soundUrl, withExtension: "m4a") else { return }
        do
        {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)

            player = try AVAudioPlayer(contentsOf: url)
            //player?.delegate = self
            guard let player = player else { return }
            player.prepareToPlay()
            player.play()
            audioPlayerDidFinishPlaying(player)



            print("play")
        } catch let error {
            print(error.localizedDescription)
        }
    }
}

extension Card: AVAudioPlayerDelegate  {
    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer){
        if let del = delegate{
            del.isOn()
        }else{
            print("the delegate is not set")
        }

    }
}

The code for my ViewController that I am trying to communicate with is below. When the image tapped function is called, the sound plays and UserInteractions should be false until the AVAudioPlayer is finished (isOn function is called).

class SecondViewController: UIViewController , UIGestureRecognizerDelegate, isOnProtocol  {
    @IBOutlet weak var imgPhoto: UIImageView!

    var card: Card!

    override func viewDidLoad() {
        super.viewDidLoad()

        card.delegate = self  //error is thrown here
    }

    func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        imgPhoto.isUserInteractionEnabled = false
        itemList[imageIndex].playSound()
    }

    func isOn() {
        imgPhoto.isUserInteractionEnabled = true
    }    
}

回答1:


Since card is defined as force-unwrapped optional:

var card: Card!

Line:

card.delegate = self

Is the same as if you force unwrapped it:

card!.delegate = self

Problem is that card is nil at that point. You have to initialize it to some value before you can use it. It has nothing to do with the Card implementation itself.

E.g.:

override func viewDidLoad() {
    super.viewDidLoad()

    card = Card(image: UIImage(), soundUrl: "url", isActive: true)

    card.delegate = self
}

Anyway, I would recommend to either use optional (Card?), or initialize the card from the very beginning and not use optional at all. If you have to use optional, using Card? will force you to always remember that the card can point to nil, and will force you to handle it somehow.



来源:https://stackoverflow.com/questions/49453402/setting-delegate-to-self-throwing-error-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!