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

        }

        

        dispatch_once(&Static.onceToken){

            Static.instance=TextToSpeech()

        }

        

        return Static.instance!

    }

    

    

    func pleaseSpeechThisText(text:NSString){

        speechUlt=AVSpeechUtterance(string: text)

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

        speechUlt.rate=0.3

        speechSyn.speakUtterance(speechUlt)

        

    }

    

}

Call this class as

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

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: