Objective-C单例实现
生活随笔
收集整理的這篇文章主要介紹了
Objective-C单例实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#ifndef RadishNinja_Singleton_h
#define RadishNinja_Singleton_h// 單例聲明(在@interface中)
#define SingletonDeclare(className)\
+(className*) singleton// 單例實現(@implementation中)
#define SingletionImplement(className) \
static className* _shared##className = nil; \
+(className*) singleton \
{ \@synchronized([className class]) \{ \if (!_shared##className) \[[className alloc] init]; \\return _shared##className; \} \\return nil; \
} \\
+(id)alloc\
{ \@synchronized([className class]) \{ \NSAssert(_shared##className == nil, @"Attempted to allocate a second instance of a singleton.");\_shared##className = [super alloc]; \return _shared##className; \} \\return nil; \
}\#endif
[[myClass singleton] doSomething];
例子:
@interface myClass : NSObjectSingletonDeclare(myClass);-(void) doSomething;@end@implementation myClassSingletionImplement(myClass);-(void) doSomething {NSLog(@"Are you sure you're a programing monkey?"); }@end[[myClass singleton] doSomething];
轉載于:https://www.cnblogs.com/iapp/archive/2012/05/04/3631804.html
總結
以上是生活随笔為你收集整理的Objective-C单例实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iphone内存检测
- 下一篇: linux 视频相关资源