Search code examples
objective-csingletonavaudiorecorder

Functions inside singleton?


How do I put a function into the singleton? i.e. so that when I use a button in another view controller, it calls the function from the singleton object file?

My current code is this:

    + (RecorderData *)sharedAudioRecorder
{
    static dispatch_once_t pred;
    static RecorderData *sharedInstance = nil;
    dispatch_once(&pred, ^
    {
        sharedInstance = [[RecorderData alloc] init];

    });
    return sharedInstance;
}

Solution

  • Simply add functions to your file and make sure you're always using sharedInstance (by calling the class method you've posted) rather than instantiating multiple instances of your class. To elaborate, your singleton object file might have an instance method like this:

    - (void) someInstanceMethod
    {
        // code here
    }
    

    ...which you could then call on the singleton like so:

    [[RecorderData sharedInstance] someInstanceMethod];