'(NSObject) - > () - >的ViewController()'不符合预期的类型' classDelegate'

时间:2017-03-08 16:45:30

标签: ios swift delegates audiokit

我是OO /移动编程的新手,我正在制作一个能够进行声音分析的应用程序(试图尽快联系调谐器应用程序)。

我有时会对OO感到困惑,不能否认它。 正如副标题所说,我得到了这个错误并且不知道为什么。让我们看看代码ViewController():

    import UIKit
    import AudioKit

    class ViewController: UIViewController, SoundAnalyserDelegate{

        let soundAnalyser = SoundAnalyser(delegate: self) //getting error here



        @IBOutlet var frequencyLabel: UILabel!
        @IBOutlet var amplitudeLabel: UILabel!
        @IBOutlet var note: UILabel!

        override func viewDidLoad() {
            super.viewDidLoad()

            soundAnalyser.start()

这部分代码启动SoundAnalyser类并委托自己。

查看SoundAnalyser代码:

    class SoundAnalyser {

        var delegate:SoundAnalyserDelegate?

        let minimum = Double(160)
        let maximum = Double(1400)

        var mic: AKMicrophone
        var tracker: AKFrequencyTracker
        fileprivate var silence: AKBooster
        fileprivate var timer: Timer?
        fileprivate var notesArray: [(String, Double)]?

        init(delegate: SoundAnalyserDelegate){
            mic = AKMicrophone()
            tracker = AKFrequencyTracker(mic, hopSize: minimum, peakCount: maximum)
            silence = AKBooster(tracker, gain:0)
            notesArray = self.getNotesFrequencies()
            self.delegate = delegate

        }


        func start(){
            AudioKit.output = silence        

在SoundAnalyser()中,我尝试从ViewController调用一个函数:

        func tick(){
            let amp = tracker.amplitude
            let freq = tracker.frequency


            if amp > 0.05 && freq > minimum && freq < maximum{
                let readNote = notesArray![analyseCapturedSound()]

                delegate?.setLabelAmp(String(format:"%f", amp)) //Setting the text of a label from the UI

                delegate?.setLabelFreq(String(format:"%f", freq))

    //            delegate?.setLabelNote(String(format:"%f", readNote))

                print("freq:", freq," amp:", amp," NOTE: \(readNote)")
            }
        }        

从OO的角度来看,这是非常错误的吗?或者我忘了什么?

我的意思是......从父亲那里打电话给一个人。上课......似乎不是最好的OO方式,但它是我唯一能做到的。

如果需要,您可以在此处查看项目:

https://github.com/denisb411/Microphone_Analyser_AK

提前致谢。

1 个答案:

答案 0 :(得分:1)

使用lazy var初始化您的对象。

class ViewController: UIViewController, SoundAnalyserDelegate{

    lazy var fontSize : SoundAnalyser = {
        return SoundAnalyser(delegate: self) 
    }()

    @IBOutlet var frequencyLabel: UILabel!
    @IBOutlet var amplitudeLabel: UILabel!
    @IBOutlet var note: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        soundAnalyser.start()

    }