标签:
#import <Foundation/Foundation.h>
@interface Person : NSObject
+ (instancetype)sharePerson;
@end
@implementation Person
+ (instancetype)sharePerson{
Person *p = [[Person alloc] init];
return p;
}
static Person *_instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if(_instance == nil){
_instance = [[super allocWithZone:zone] init];
}
return _instance;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *p1 = [[Person alloc] init];
Person *p2 = [Person new];
Person *p3 = [Person sharePerson];
// 如果打印出的3个地址一样,说明单例设计成功
NSLog(@"%p --- %p --- %p", p1, p2, p3);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/lianfu/p/5172104.html