static PaperPack *_paperPack;
@interface PaperPack ()
@property (nonatomic, assign) NSInteger createdCount;
- (PaperPack *)sharedPaperPack;
@end
@implementation PaperPack
+ (PaperPack *)sharedPaperPack
{
@synchronized(self)
{
if(!_sharedPaperPack)
{
_sharedPaperPack = [[[self class] alloc] init];
}
}
return _sharedPaperPack;
}
+ (PaperPack*)paperPack {
self = [super init];
if (self) {
[PaperPack sharedPaperPack].createdCount ++;
}
return self;
}
使用するには:
「createdCount」値を増やすクラスメソッドを呼び出すだけです
PaperPack *firstPaperPack = [PaperPack paperPack];
PaperPack *secondPaperPack = [PaperPack paperPack];
そして数えます:
NSInteger count = [PaperPack sharedPaperPack].createdCount;
何かが間違っていたらごめんなさい、コードはメモリから書かれています