Making a class singleton in Swift

Making a class singleton  in Swift

Create a singleton class

import UIKit

import AVFoundation

class TextToSpeech: NSObject {


    let speechSyn=AVSpeechSynthesizer()

    var speechUlt=AVSpeechUtterance(string: “”)



    class var shareInstance:TextToSpeech{

      struct Static{

        static var onceToken:dispatch_once_t=0

        static var instance:TextToSpeech?=nil







        return Static.instance!




    func pleaseSpeechThisText(text:NSString){

        speechUlt=AVSpeechUtterance(string: text)

        speechUlt.voice = AVSpeechSynthesisVoice(language: “en-au”)







Call this class as

TextToSpeech.shareInstance.pleaseSpeechThisText(“Hi I am an iOS Developer”)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: