OC自定义单例

自定义一个MeloPerson对象,使之成为单例。

// 重写allocWithZone:方法,保证只会分配一次内存空间

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static MeloPerson *instance;
    static dispatch_once_t onceToken;
    dispatch_once (&onceToken, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
}
// 提供一个sharedMeloPerson方法,供其他类访问
+ (instancetype)sharedMeloPerson
{
    return [[self alloc] init];
}

posted @ 2014-08-02 23:14  Jeremy1988  阅读(94)  评论(0)    收藏  举报