Tag: Swift

Swift: how to create a Singleton pattern

What is a Singleton?

A singleton pattern guarantees that only one instance of a class is initialized and available from different points of an app.

Some examples are already available in Apple’s frameworks:

// Shared URL Session
let sharedURLSession = URLSession.shared

// Default File Manager
let defaultFileManager = FileManager.default

// Standard User Defaults
let standardUserDefaults = UserDefaults.standard

How to define a Singleton

Often a static constant is used to adopt the Singleton pattern. To do that the reference to the shared instance is stored inside a static constant. In addition, the initializer is private and therefore hidden:

class NetworkManager {
    static let sharedInstance = NetworkManager()

    private init() { }

    func doSomething() {
        // ...

By making the initializer method private, it’s guaranteed that no other instance of this class can be created. It would also be possible to move the static constant outside the class, but then the initializer might be accessible and that’s not what we want.

To access our NetworkManager instance just call:


This approach allows to use always the same instance, even in different points of the app.

Xcode fails to generate source files from intent definition files when using the Legacy Build System

The workaround for this problem is the following: First, add a Run Script phase before the Compile Sources phase of your target:

xcrun intentbuilderc generate -input ${SRCROOT}/PATH/TO/Intents.intentdefinition -output ${SRCROOT}/Intents -classPrefix "" -language Swift -swiftVersion 5.0

Then, add all of the generated files from the output path specified in the command above to all required targets in your project.

Source: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_release_notes

Swift: Audioaufnahmen mit AVAudioRecorder

In Swift lassen sich Audioaufnahmen sehr komfortabel und einfach über die Klasse AVAudioRecorder realisieren. Beim Initialisieren der Klasse lassen sich bereits Codec, Samplerate, Anzahl der Kanäle und einige andere wichtige Einstellungen festlegen. Hier ein einfaches Code-Beispiel:

class AudioViewController : UIViewController, AVAudioRecorderDelegate
    // [...]
    var audioRecorder: AVAudioRecorder!
    func initAudioRecorder()
        let settings: [String : Any] = [
            AVSampleRateKey: 16000.0,
            AVFormatIDKey: kAudioFormatMPEG4AAC,
            AVNumberOfChannelsKey: 1,
            AVEncoderAudioQualityKey: AVAudioQuality.low.rawValue
        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSessionCategoryRecord)
            try audioRecorder = AVAudioRecorder(url: getFileurl()!, settings: settings)
            try audioSession.setActive(true)
        catch let error as NSError {
            print("AVAudioRecorder error: \(error.localizedDescription)")
        audioRecorder.delegate = self
        audioRecorder.isMeteringEnabled = true
        let displayLink:CADisplayLink = CADisplayLink(target: self, selector: #selector(updateMeters))
        displayLink.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
    func updateMeters()
        if audioRecorder.isRecording
            let time = NSInteger(audioRecorder.currentTime)
            // ...
        // TOOD: do any updates e.g. for the UI (update displayed record time, ...)
    @IBAction func startRecord(_ sender: AnyObject)
    @IBAction func stopRecord(_ sender: AnyObject)
    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool)
        // Add audio to attachments
        let audioData = try? Data(contentsOf: getFileurl()!)
        if audioData != nil
            // TOOD: handle audio data
    func getFileurl() -> URL?
        let fileManager = FileManager.default
        let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = urls[0] as URL
        let audioURL = documentDirectory.appendingPathComponent("audio.m4a")
        return audioURL