Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347196
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: BSD

2011-10-18 08:36:26

@interface MySingleton : NSObject {
 
       
// ...
 
}
 
+ (MySingleton *)sharedInstance;
 
// Interface
- (NSString *)helloWorld;
 
@end#import "MySingleton.h"
 
static MySingleton *sharedInstance = nil;
 
 
@implementation MySingleton
 
 
#pragma mark Singleton methods
 
+ (MySingleton *)sharedInstance {
 
   
@synchronized(self) {
       
if (sharedInstance == nil) {
                        sharedInstance
= [[MySingleton alloc] init];
               
}
   
}
 
   
return sharedInstance;
}
 
+ (id)allocWithZone:(NSZone *)zone {
   
@synchronized(self) {
       
if (sharedInstance == nil) {
            sharedInstance
= [super allocWithZone:zone];
           
return sharedInstance;  // assignment and return on first allocation
       
}
   
}
   
return nil; // on subsequent allocation attempts return nil
}
 
- (id)copyWithZone:(NSZone *)zone {
   
return self;
}
 
- (id)retain {
   
return self;
}
 
- (unsigned)retainCount {
   
return UINT_MAX;  // denotes an object that cannot be released
}
 
- (void)release {
   
//do nothing
}
 
- (id)autorelease {
   
return self;
}
 
#pragma mark -
#pragma mark NSObject methods
 
- (id)init {
       
if (self = [super init]) {
               
// ...
       
}
       
return self;
}
 
- (void)dealloc {
       
// ...
 
       
[super dealloc];
}
 
#pragma mark -
#pragma mark Implementation
 
- (NSString *)helloWorld {
       
return @"Hello World!";
}
 
@end#import "MySingleton.h"
 
// ...
 
NSLog(@"Result for singleton method helloWorld: %@", [[MySingleton sharedInstance] helloWorld]);
阅读(4996) | 评论(0) | 转发(0) |
0

上一篇:protocol --000

下一篇:检查网络是否存在

给主人留下些什么吧!~~