Swift3 无法为我的类创建单例对象

Swift3 无法为我的类创建单例对象,swift3,Swift3,我正在为Text2Speech创建一个类,为此,我正在创建Singleton对象,以便在我的项目中的许多地方将其用作单个对象,但我得到以下错误: 我的代码: static let sharedInstance = Text2Speech() 或 我收到的错误: class Text2Speech: NSObject, AVSpeechSynthesizerDelegate { var delegate: Text2SpeechDelegate? var utteranceCou

我正在为Text2Speech创建一个类,为此,我正在创建Singleton对象,以便在我的项目中的许多地方将其用作单个对象,但我得到以下错误:

我的代码:

static let sharedInstance = Text2Speech()

我收到的错误:

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)
    }
}
无法在属性初始值设定项中使用实例成员“Text2Speech”; 属性初始值设定项在“self”可用之前运行

我在很多地方对SingleTon对象做了相同的声明,但只有在这个类中我得到了上述错误

我已经检查过了,但没有找到解决方案

那门课上有什么乱七八糟的东西

编辑:

Text2Speech类:

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)
    }
}
有人能帮我吗


提前感谢。

问题是您有一个与 班级。下面是一个最小的自包含示例,演示 问题:

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) { }
}
显然,编译器将
Text2Speech()
作为一种尝试 调用函数。重命名函数(例如,将函数重命名为
func text2Speech
)可以解决此问题

请注意,Swift惯例是以
大写字母和带有小写字母的函数名。

问题是您有一个与 班级。下面是一个最小的自包含示例,演示 问题:

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) { }
}
显然,编译器将
Text2Speech()
作为一种尝试 调用函数。重命名函数(例如,将函数重命名为
func text2Speech
)可以解决此问题

请注意,Swift惯例是以
大写字母和带有小写字母的函数名。

需要更多上下文
class Text2Speech{static let sharedInstance=Text2Speech()}
编译没有问题。@MartinR我已经添加了整个Text2Speech类。你能检查我更新的答案吗?需要更多的上下文
class Text2Speech{static let sharedInstance=Text2Speech()}
编译没有问题。@MartinR我已经添加了整个Text2Speech类,你能检查我更新的答案吗?是的@Martin你是对的,它的问题是函数名和类名相同。现在它工作了,我把它改成了
func text2Speech
,现在它工作了,谢谢你的快速回复。是的@Martin你是对的,它的问题是函数名和类名相同。现在它工作了,我把它改成了
func text2Speech
,现在它工作了,谢谢你的快速回复。