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.