我收到错误' EXC_BAD_ACCESS(code = 2,address = 0x16fcbbfec)'在我的TimeView控制器上
var soundEffects = SoundEffects()
我不知道为什么会这样......
我已经为任何愿意看看并帮助我的人提供了我的源代码链接。谢谢!
https://github.com/JohnnyH1012/HRC
相关代码
import UIKit
import AVFoundation
class SoundEffects: NSObject, AVAudioPlayerDelegate {
var soundEffects = SoundEffects()
var player = AVAudioPlayer()
var storage = SettingsStorage()
var enabled:Bool = false
var newValue:Bool = true
var tabata: Tabata!
var theme: Theme!
func registerSoundEffects() {
soundEffects = SoundEffects.new()
NSNotificationCenter.defaultCenter().addObserver(soundEffects, selector: "stateChanged:", name: StateChanged, object: nil)
NSNotificationCenter.defaultCenter().addObserver(soundEffects, selector: "prepareSignal:", name: PrepareSignal, object: nil)
var error:NSError?
var url: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("beep_01", ofType: "mp3")!)!
player = AVAudioPlayer(contentsOfURL: url, error: &error)
player.numberOfLoops = 0
player.prepareToPlay()
enabled = storage.loadSoundEnabled()
}
func isEnabled() {
return enabled = true
}
func setEnabled() {
enabled = newValue
storage.saveSoundEnabled(newValue)
}
func stateChanged(notification: NSNotification) {
if enabled {
var tabata: Tabata! = notification.object! as! Tabata
switch tabata.getState() {
case .EXERCISE: fallthrough
case .RELAXATION:
player.play()
break
default:
break
}
}
}
func prepareSignal(notification:NSNotification) {
if enabled {
player.play()
}
}
}
答案 0 :(得分:0)
你有一个无限的递归循环。初始化SoundEffects
时,它会初始化其soundEffects
变量,该变量初始化SoundEffects
对象,该对象初始化soundEffects
var,其中......
你看到了问题吗?
答案 1 :(得分:0)
可能与setEnabled
二传手有冲突。在Swift中有属性观察者,它们在设置变量后被调用。
删除isEnabled()
和setEnabled()
方法使用变量enabled
这样也作为getter
var enabled: Bool = false {
didSet {
storage.saveSoundEnabled(newValue)
}
}