English 中文(简体)
Apple果单州的例子?
原标题:Apple Singleton example query?

我感到困惑不解的是,在创建单一州法院时,这一法典(载于《科恰丰通斯指南》)压倒了某些方法。

static id sharedReactor = nil;

+(id)sharedInstance {
    if(sharedReactor == nil) sharedReactor = [[super allocWithZone:NULL] init];
    return sharedReactor;
}

+(id)allocWithZone:(NSZone *)zone {
    return[[self sharedInstance] retain];
}

-(id)retain {
    return self;
}

In the code where the singleton instance is created the +sharedInstance method calls [super allocWithZone:NILL] from the superclass (which in my case is NSObject) The allocWithZone above is only called if you attempt to use it to create a new singleton。

The bit I am confused about is the use of retain, especially seeing as retain is also overridden to return self。 Can anyone explain this, could it not be written:

+(id)allocWithZone:(NSZone *)zone {
    return [self sharedInstance];
}

-(id)retain {
    return self;
}

<>strong>EDIT_001:

Based on comments and reading various posts on the web I have decided to go with the following (see below) I have chosen to go for a shared singleton approach where if needed I would have the option of creating a second or third instance。 Also at this stage as I am only using the singleton for the model portion of MVC for a simple iPhone app I have decided to leave thread safety out。 I am aware its important and as I get more familiar with iPhone programming I will likely use +initialize instead (keeping in mind the subclass issue where it can be called twice) Also I have added a dealloc, firstly to log a message should the singleton be released, but also to clean things up properly should the singleton be no longer required。

@interface SharedManager : NSObject
+(id)sharedInstance;
@end

@implementation SharedManager

static id myInstance = nil;

+(id)sharedInstance {
    if(myInstance == nil) {
        myInstance = [[self alloc] init];
    }
    return myInstance;
}

-(void)dealloc {
    NSLog(@"_deal: %@", [self class]);
    [super dealloc];
    myInstance = nil;
}
@end

In testing I found that I had a set the static variable to nil in the dealloc or it maintained its pointer to the original object。 I was initially a little confused by this as I was expecting the scope of the static to be the instance, I guess its the class instead, which makes sense。

厨师

最佳回答

首先,不使用该法典。 几乎永远没有理由在一个简单的单一州这样做。 Apple果正在显示“被迫单一吨”,因为不可能创造其中两个。 真正需要这一点非常少见。 你们几乎总是能够使用大多数有单一州建筑的可可物体所使用的“共有单一吨”方法。

在此,我倾向于执行共用单一州:

+ (MYManager *)sharedManager
{
    static MYManager *sharedManager = nil;
    if (sharedManager == nil)
    {
        sharedManager = [[self alloc] init];
    }
    return sharedManager;
}

这就是说。 不需要其他法典。 使用<代码>+共享dManager的传票人将获得共同的认可。 打电话到<代码>+alloc的传票人如果真想的话,可以创造独特的事例。 这正是这些著名的“袖珍”工作(NS NotificationCenter)。 如果你真的想要你自己的私人通知中心,那类人就没有理由拒绝。 这种办法具有以下优势:

  • Less code.
  • More flexible in cases where a non-shared instance is useful.
  • Most importantly: the code does what it says it does. A caller who thinks he s making a unique instance with +alloc doesn t encounter surprising "spooky action at a distance" behavior that requires him to know an internal implementation detail of the object.

如果你真的need一个强迫的单一吨位,因为所涉物体被描绘成无法分享的独特资源(而且确实很少遇到这种情况),那么你仍应使用t <+alloc的trick滴加以执行。 这只是掩盖了试图制造新情况的方案拟订错误。 相反,你应该以这种方式应对方案拟订错误:

+ (MYManager *)sharedManager
{
    static MYManager *sharedManager = nil;
    if (sharedManager == nil)
    {
        sharedManager = [[self alloc] initSharedManager];
    }
    return sharedManager;
}

- (id)init
{
    NSAssert(NO, @"Attempting to instantiate new instance. Use +sharedManager.");
    return nil;
}

// Private method. Obviously don t put this in your .h
- (id)initSharedManager
{
    self = [super init];
    ....
    return self;
}
问题回答

There is a good example of different singleton methods with comments here on SO: What does your Objective-C singleton look like?

如果它有所帮助,那么这个例子就对谁是谁是谁返回了。





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签