@interface MySingleClass (private)
 + (id)hiddenAlloc;
@end

@implementation MySingleClass
+(id)hiddenAlloc
{
    return [super alloc];
}

+ (id) alloc
{
    NSLog(@"%@ : use +sharedInstance instead of +alloc", [[self class] name]);
    return nil;


+ (id) new
{
    return [self alloc];
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [self alloc];
}

- (id) copyWithZone:(NSZone *)zone
{
    NSLog(@"MySingleClass: attempt to -copy may be a bug.");
    [self retain];
    return self;
}

- (id) mutableCopyWithZone:(NSZone *)zone
{
    return [self copyWithZone:zone];
}

+ (MySingle *)sharedInstance
{
    static MySingleClass * myInstance = nil;
    if (!myInstance){
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSDictionary *info = [mainBundle infoDictionary];
        NSString *className = [info objectForKey:@"MYSingleClass"];
        Class *myClass = NSClassFromString(className);
        if (!myClass) {
            myClass = self;
        }
        myInstance = [[myClass hiddenAlloc] init];
    }
    return myInstance;
}

  + (void) attemptDealloc
{
    if ([myInstance retainCount] != 1) return;
    [myInstance release];
    myInstance = nil;
}

@end 


'iOS > Object C' 카테고리의 다른 글

[펌] NSArray, NSDictionary 초기화  (0) 2014.09.26
Objective C 싱글톤  (0) 2012.01.26
Selector  (0) 2012.01.12
[펌] Object C 타입간 변환  (0) 2012.01.02
[펌] Objective-C 기초  (0) 2011.12.21
[펌] NSString & NSMutableString  (0) 2011.12.13
Posted by 까칠코더.


티스토리 툴바