怎么写好单例

import “SingleObj.h”

static SingleObj *kSingleObj = nil;

@implementation SingleObj

  • (instancetype)sharedInstance
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    kSingleObj = [[super allocWithZone:NULL] init];
    });
    return kSingleObj;
    }

  • (instancetype)allocWithZone:(struct _NSZone *)zone
    {
    return [self sharedInstance];
    }

  • (id)copy
    {
    // return kSingleObj;
    return [self.class sharedInstance];
    }

  • (id)mutableCopy
    {
    // return kSingleObj;
    return [self.class sharedInstance];
    }

你可能感兴趣的:(iOS)