我创建了一个单例类,如何从ViewControllers中调用它?

时间:2022-12-02 20:23:43

I've created the following singleton class, but I'm not sure how to call it from my ViewControllers (sorry for the newb question)? I've imported it into my ViewController (#import cloudStore.h), but what line of code should I be using in a ViewController's viewDidLoad in order to "use" it?

我创建了以下单例类,但我不确定如何从我的ViewControllers中调用它(抱歉新问题)?我已经将它导入我的ViewController(#import cloudStore.h),但我应该在ViewController的viewDidLoad中使用哪一行代码来“使用”它?

cloudStore.h

cloudStore.h

#import <Foundation/Foundation.h>

@interface cloudStore : NSObject

+(cloudStore *)singleton;

@end

cloudStore.m

cloudStore.m

#import "cloudStore.h"

@implementation cloudStore

+(cloudStore *)singleton {
    static dispatch_once_t pred;
    static cloudStore *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[cloudStore alloc] init];
    });
    return shared;
}

@end

1 个解决方案

#1


2  

you should do

你应该做

cloudStore *aCloudStore = [cloudStore singleton];

in order to get a reference to your single instance of cloudStore.

为了获得对您的单个cloudStore实例的引用。

#1


2  

you should do

你应该做

cloudStore *aCloudStore = [cloudStore singleton];

in order to get a reference to your single instance of cloudStore.

为了获得对您的单个cloudStore实例的引用。