在Swift中将委托设置为自抛出错误 [英] setting delegate to self throwing error in Swift
问题描述
我目前正在研究在播放AVAudioPlayer时禁用UIImageView的代码.我要禁用的AVAudioPlayer和ImageView位于两个不同的类中.我尝试编写一个委托以使两个类进行通信,但是在尝试在viewController中分配给self时,我继续遇到错误线程1:致命错误:在展开可选值时意外发现nil".
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.
包含我的AVAudioPlayer的类的代码如下
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")
}
}
}
我尝试与之通信的ViewController的代码如下.调用图像点击功能时,声音会播放,并且UserInteractions应该为false,直到AVAudioPlayer完成(调用isOn函数)为止.
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
}
}
推荐答案
由于card
被定义为强制展开的可选内容:
Since card
is defined as force-unwrapped optional:
var card: Card!
行:
card.delegate = self
与您强行解开包装的情况相同:
Is the same as if you force unwrapped it:
card!.delegate = self
问题是此时card
是nil
.您必须先将其初始化为某个值,然后才能使用它.它与Card
实现本身无关.
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.
例如:
override func viewDidLoad() {
super.viewDidLoad()
card = Card(image: UIImage(), soundUrl: "url", isActive: true)
card.delegate = self
}
无论如何,我建议要么使用可选的(Card?
),要么从一开始就初始化card
,而根本不使用可选的.如果必须使用可选,则使用Card?
会迫使您始终记住card
可以指向nil,并且会迫使您以某种方式进行处理.
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.
这篇关于在Swift中将委托设置为自抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!