1

I am trying to follow a tutorial for interfacing Game Center, that uses the Apple Documentation and Matt Gallagher's "singleton" document.

However, XCode is emitting polite but vehement protests, lodging its complaints as "Data definition has no type or storage class". In other words, it thinks that SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER(GameCenterManager); is a method declaration lacking a type. Some StackOverflow archaeology brought to my attention a change to XCode.

However, it lets me quite perplexed. Should I really create, deep inside the compiler settings, two entries, one of which is at least 600 characters long, that basically will contain what currently is inside a nice, neat header file?

Follows the source of said header file, written by Matt Gallagher, which would then go into two places:

#define SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER(__CLASSNAME__)    \
\
+ (__CLASSNAME__ *)shared##__CLASSNAME__;   \
+ (void)purgeShared##__CLASSNAME__;

and

#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
 \
+ (classname *)shared##classname \
{ \
@synchronized(self) \
{ \
    if (shared##classname == nil) \
    { \
        shared##classname = [[self alloc] init]; \
    } \
} \
 \
return shared##classname; \
} \
 \
+ (id)allocWithZone:(NSZone *)zone \
{ \
@synchronized(self) \
{ \
    if (shared##classname == nil) \
    { \
        shared##classname = [super allocWithZone:zone]; \
        return shared##classname; \
    } \
} \
 \
return nil; \
  } \
  \
 - (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
} \
 \
- (id)retain \
{ \
return self; \
} \
 \
- (NSUInteger)retainCount \
{ \
return NSUIntegerMax; \
} \
 \
- (void)release \
{ \
} \
 \
- (id)autorelease \
{ \
return self; \
}

So, where am I mistaken in my understanding?

Community
  • 1
  • 1
Kheldar
  • 5,361
  • 3
  • 34
  • 63
  • I'd question any design where singletons are so common that such a #define is needed (not to mention the drama involved in that singleton implementation). It is likely to make any kind of future refactoring much more difficult. – bbum Sep 20 '11 at 17:26
  • Well, I agree with that, but Gallagher points out that code duplication is bad, and you might need several Singletons anyway. So I'll follow his lead, he's more savvy than I :D – Kheldar Sep 20 '11 at 19:58
  • 1
    If code duplication is a sin, so is complexity. Every singleton I've ever implemented has consisted of a single method; `+sharedInstance`. That is all that is needed and the rest is noise, hides bugs, and makes refactoring more difficult in the future. – bbum Sep 20 '11 at 20:51

1 Answers1

0

I added this line into the source file and now it runs fine. As I understand it, if I want to define something I have to use this import to notify the compiler I'm aware of what I'm doing.

#import <objc/runtime.h>

If I'm wrong, feel free to correct me.

Kheldar
  • 5,361
  • 3
  • 34
  • 63