.h文件
#import <Foundation/Foundation.h>
@interface Person : NSObject
+ (instancetype) sharePerson;
@end
.m文件
#import "Person.h"
@implementation Person
static Person *instance = nil;
+ (void)load {
NSLog(@"%s", __func__);
instance = [[self alloc] init];
}
+ (instancetype)sharePerson {
return instance;
}
/**
* 当实例化时抛出异常
*/
+ (instancetype)alloc{
if (instance) {
/**
* name: 异常的名称
*
* reason: 异常产生的原因
*
* userInfo: 异常的信息
*/
NSException *exception = [NSException exceptionWithName:@"NSInteranlInconsistencyException" reason:@"There can only be one Person instance." userInfo:nil];
[exception raise];
}
return [super alloc];
}
@end