我已经找到了如何在目标 c(非 ARC)中实现单例。
// AppTools.h in my code
@interface AppTools : NSObject {
    NSString *className;
}
@property ( nonatomic, retain ) NSString *className;
+ ( id ) sharedInstance;
@end    // AppTools
// AppTools.m in my code
static AppTools *sharedAppToolsInstance = nil;
@implementation AppTools
@synthesize className;
- ( id ) init {
    self = [ super init ];
    if ( self ) {
        className = [ [ NSString alloc ] initWithString: @"AppTools" ];
    }
    return self;
}   // init
- ( void ) dealloc {
   // Should never be called, but just here for clarity really.
   [ className release ];
   [ super dealloc ];
}   // dealloc
+ ( id ) sharedInstance {
    @synchronized( self ) {
    if ( sharedAppToolsInstance == nil )
        sharedAppToolsInstance = [ [ super allocWithZone: NULL ] init ];
    }
    return sharedAppToolsInstance;
}   // sharedInstance
+ ( id ) allocWithZone: ( NSZone * )zone {
    return [ [ self sharedInstance ] retain ];
}   // allocWithZone:
- ( id ) copyWithZone: ( NSZone * )zone {
    return self;
}   // copyWithZone:
- ( id ) retain {
    return self;
}   // retain
- ( unsigned int ) retainCount {
    return UINT_MAX;    // denotes an object that cannot be released
}   // retainCount
- ( oneway void ) release {
    // never release
}   // release
- ( id ) autorelease {
    return self;
}   // autorelease
我想知道如何在 sharedInstance 方法中使用 allocWithZone:。在这方面,allocWithZone: 方法的接收者是 'super' 而 'super' 是 NSObject。虽然返回值是 NSObject 实例,但它被替换为 sharedInstance。
那么className的记忆室在哪里呢?我不知道如何处理这部分代码。
预先感谢。