0

I am creating one class for Text2Speech and for that, I am creating a Singleton object to use it as a single object at many places in my project but I am getting the below error:

My Code:

static let sharedInstance = Text2Speech()

Or

static let sharedInstance : Text2Speech = {
    let instance = Text2Speech()
    return instance
}()

Error I am getting:

Cannot use instance member 'Text2Speech' within property initializer; property initializers run before 'self' is available

I am doing the same declaration for the SingleTon object at many places but Only in this class, I am getting the above error.

I have already checked this question thread but no luck on any solution.

Something wired or messed in that class.

Edit:

Text2Speech Class:

class Text2Speech: NSObject, AVSpeechSynthesizerDelegate {
    var delegate: Text2SpeechDelegate?
    var utteranceCount : Int
    var isMale : Bool
    let synth = AVSpeechSynthesizer()
    static let sharedInstance = Text2Speech()
    
//    static let sharedInstance : Text2Speech = {
//        let instance = Text2Speech()
//        return instance
//    }()
    
    override init() {
        self.utteranceCount = 0;
        self.isMale = false
        super.init()
        synth.delegate = self
    }
    
    func Text2Speech(_ text: String){
        utteranceCount += 1
        let myUtterance = AVSpeechUtterance(string: text)
        myUtterance.postUtteranceDelay = 0.005
        myUtterance.rate = 0.5
        myUtterance.pitchMultiplier = 1.0
        if self.isMale{
            myUtterance.voice = AVSpeechSynthesisVoice(language:"en-GB")
        }
        synth.speak(myUtterance)
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
        delegate?.Text2SpeechConvertor(synthesizer, didStart: utterance)
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        utteranceCount -= 1
        let seriouslyFinished = utteranceCount==0 ? true:false
        delegate?.Text2SpeechConvertor(synthesizer, didFinish: utterance, seriouslyFinished: seriouslyFinished)
    }
}

Can anyone help me with this?

Thanks in advance.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80

1 Answers1

2

The problem is that you have a function with the same name as the class. Here is a minimal self-contained example demonstrating the problem:

class Text2Speech {
    static let sharedInstance = Text2Speech()
    // error: cannot use instance member 'Text2Speech' within property initializer; property initializers run before 'self' is available

    func Text2Speech(_ text: String) { }
}

Apparently, the compiler takes Text2Speech() as an attempt to call the function. Renaming the function (e.g. to func text2Speech) solves the issue.

Note that the Swift convention is to start type names with uppercase letters and function names with lowercase letter.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Yes @Martin you are right its problem of Same function name as Class name. Now its working i have changed it to `func text2Speech` and its working now, Thanks for your quick response. – CodeChanger Aug 23 '17 at 10:49