leftshift
Mitglied
Thread Starter
- Dabei seit
- 24.02.2009
- Beiträge
- 45
- Reaktionspunkte
- 6
Hallöchen,
ich arbeite gerade das obige Buch durch und hänge am Singleton fest. In C++/Java kein Problem aber in Objective-C?!
Hier ist mal die Implementierung:
Er springt auch brav in die Methode 'hiddenAlloc' bekommt bei '[super alloc]' aber einen Nullpointer.
Hat einer diese Klasse lauffähig bekommen?
leftshift
ich arbeite gerade das obige Buch durch und hänge am Singleton fest. In C++/Java kein Problem aber in Objective-C?!
Hier ist mal die Implementierung:
Code:
@interface Singleton : NSObject
{
@private
int m_valueX;
}
+ (id)sharedInstance;
- (id)init;
- (int)valueX;
@end
#import "Singleton.h"
@implementation Singleton
// private Methoden
+ (id)hiddenAlloc
{
return [super alloc];
}
+ (id)alloc
{
return nil;
}
+ (id)new
{
return [self alloc];
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self alloc];
}
- (id)copyWithZone: (NSZone*)zone
{
NSLog(@"Singleton: attempt to -copy may be a bug.");
[self retain];
return self;
}
- (id)mutableCopyWithZone: (NSZone*)zone
{
return [self copyWithZone:zone];
}
+ (Singleton*)sharedInstance
{
static Singleton *theInstance = nil;
if( !theInstance )
{
theInstance = [[[self class ] hiddenAlloc] init];
}
return theInstance;
}
- (id)init
{
self = [super init];
m_valueX = 99;
return self;
}
- (int)valueX
{
return m_valueX;
}
@end
Aufruf mit: [Singleton sharedInstance];
Er springt auch brav in die Methode 'hiddenAlloc' bekommt bei '[super alloc]' aber einen Nullpointer.
Hat einer diese Klasse lauffähig bekommen?
leftshift