专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

创建 SQLiteManager 单例

  • 与网络接口的独立类似,数据库的底层操作,也应该有一个独立的对象单独负责

  • 新建 SQLiteManager 类,并且实现以下代码:

    1. SQLiteManager.h

       @interface SQLiteManager : NSObject
      
       // 单例
       + (instancetype)sharedManager;
      
       @end
      
    2. SQLiteManager.m

        @implementation SQLiteManager
      
        static id _instance;
        + (instancetype)sharedManager {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                _instance = [[self alloc] init];
            });
      
            return _instance;
        }
      
        + (instancetype)allocWithZone:(struct _NSZone *)zone {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                _instance = [super allocWithZone:zone];
            });
      
            return _instance;
        }
      
        - (id)copyWithZone:(nullable NSZone *)zone {
            return _instance;
        }
      
        @end
      

results matching ""

    No results matching ""