0

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
    }    
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anthony Rubin
  • 69
  • 1
  • 1
  • 14

1 Answers1

2

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.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • Ive changed it to what you recommended, however the protocol method that is supposed to run does not run. If you look at my card class and see where it says print("the delegate is not set"). This line is printed instead of the isOn method running. Can you explain why? – Anthony Rubin Mar 23 '18 at 18:24