Qui*_*ick 2 singleton constants objective-c
美好的一天,朋友们.
从新手回来关于Obj-C的愚蠢问题:)
我正在尝试在Obj-C中实现单例设计模式:
@interface SampleSingleton : NSObject {
@private
static SampleSingleton* instance;
}
+(SampleSingleton*) getInstance;
Run Code Online (Sandbox Code Playgroud)
编译器返回错误:"在'静态'之前的预期说明符 - 限定符列表".
您不能在类接口声明中使用static.单例应该在文件中声明为静态独立变量.m.我通常这样做(如果我觉得我不能避免单身):
@interface SampleSingleton : NSObject
{
@private
}
+(SampleSingleton*) theSingleton;
@end
// .m file
@implementation SampleSingleton
+(SampleSingleton*) theSingleton
{
static SampleSingleton* theSingleton = nil;
if (theSingleton == nil)
{
theSingleton = [[SampleSingleton alloc] init];
}
return theSingleton;
}
Run Code Online (Sandbox Code Playgroud)
请在下面找到我正在使用的Objective-C代码片段,以获得正确的线程安全单例实现
头文件:
/*
*
* Singleton interface that match Cocoa recommendation
* @ http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
* extended with thread-safe pattern
*/
@interface MyCustomManager : NSObject {
}
#pragma mark Singleton Thred-Safe Pattern
+ (MyCustomManager *) sharedInstance;
+ (id)allocWithZone:(NSZone *)zone;
- (id)copyWithZone:(NSZone *)zone;
- (id)retain;
- (NSUInteger)retainCount;
- (void)release;
- (id)autorelease;
#pragma mark -
Run Code Online (Sandbox Code Playgroud)
实施文件:
/*
* My custom manager Class singleton implementation
*/
@implementation MyCustomManager
#pragma mark Initializers
/*
* specific initialize goes here
*/
- (void) specificInitialize
{
// ...
}
/*
* Ensure any owned object is properly released
*/
- (void) dealloc
{
[super dealloc];
}
#pragma mark -
#pragma mark Singleton Thred-Safe Pattern
//- use Volatile to make sure we are not foiled by CPU caches
static void * volatile sharedInstance = nil;
/*
* retrieve sharedInstance based on OSAtomicCompareAndSwapPtrBarrier that
* acts as both a write barrier for the setting thread and a read barrier from the testing thread
* more info @ http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like/2449664#2449664
* and http://stackoverflow.com/questions/6915/thread-safe-lazy-contruction-of-a-singleton-in-c/6943#6943
*/
+ (MyCustomManager *) sharedInstance {
//- check sharedInstance existenz
while (!sharedInstance) {
//- create a temporary instance of the singleton
id temp = [super allocWithZone:NSDefaultMallocZone()];
//- The OSAtomicCompareAndSwapPtrBarrier function provided on Mac OS X
//- checks whether sharedInstance is NULL and only actually sets it to temp to it if it is.
//- This uses hardware support to really, literally only perform the swap once and tell whether it happened.
if(OSAtomicCompareAndSwapPtrBarrier(0x0, (void *)temp, &sharedInstance)) {
//- compute singleton initialize
MyCustomManager *singleton = (MyCustomManager *) sharedInstance;
[singleton specificInitialize];
}
else {
//- if the swap didn't take place, delete the temporary instance
[temp release];
temp = nil;
}
}
//- return computed sharedInstance
return sharedInstance;
}
/*
* method to ensure that another instance is not allocated if someone tries to allocate
* and initialize an instance of your class directly instead of using the class factory method.
* Instead, it just returns the shared object.
*/
+ (id)allocWithZone:(NSZone *)zone
{
return [[self sharedInstance] retain];
}
/*
* Implements the base protocol methods to do the appropriate things to ensure singleton status.
* Applies to memory-managed code, not to garbage-collected code
*/
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
/*
* Implements the base protocol methods to do the appropriate things to ensure singleton status.
* Applies to memory-managed code, not to garbage-collected code
*/
- (id)retain
{
return self;
}
/*
* Implements the base protocol methods to do the appropriate things to ensure singleton status.
* Applies to memory-managed code, not to garbage-collected code
*/
- (NSUInteger)retainCount
{
return NSUIntegerMax; //denotes an object that cannot be released
}
/*
* Implements the base protocol methods to do the appropriate things to ensure singleton status.
* Applies to memory-managed code, not to garbage-collected code
*/
- (void)release
{
//do nothing
}
/*
* Implements the base protocol methods to do the appropriate things to ensure singleton status.
* Applies to memory-managed code, not to garbage-collected code
*/
- (id)autorelease
{
return self;
}
#pragma mark -
Run Code Online (Sandbox Code Playgroud)
只是为了帮助您从objective-c开始而不是迷失在您的项目结构中,您可以考虑让项目结构与您的文件系统相匹配,这样您的项目就会变得更大,您不会迷路.
另外请考虑使用适当的类命名约定,并坚持下去.
我向你提供我的样本:
任何匹配单例模式的类都使用Manager后缀(例如MyCustomManager)命名.
任何静态类都使用Helper后缀命名(例如MyCustomHelper).
任何专用于控制特定进程的类都使用Controller后缀(例如MyParticularTaskConstroller)命名.
从其他控件继承的任何UI控件都需要提供控件后缀(例如,继承自UITableViewCell的MyCustomDetailCell)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
5455 次 |
| 最近记录: |