Goal C - Custom @synthesize?

Is it possible to somehow create a custom @synthesize to generate custome getter, seters

For instance:

@interface
@property (nonatomic, retain) MyObject *object;
@end

@implementation
@lazyInitialize object;
@end

And then somehow define @lazyInitialize to create a lazy initialization method

//@lazyInitialize

- (id)"property name"
{
   if (!"property name")
   {
      "property name" = [[["property name" class] alloc] init];
   }
   return "property name";
}
+3
source share
2 answers

You could try something else. I would not have thought about this more than a couple of days ago, but I happened to read Cocoa With Love . In connection with the post, he discussed how he created a macro #definethat “generated” the entire singleton class in the place where you called the macro. You can upload your code for this (you can give ideas on your own implementation).

, - (Warning: Untested Code Ahead):

#define SYNTHESIZE_LAZY_INITIALIZER_FOR_OBJECT(objectName, objectType) \
\
- (objectType *)objectName \
{ \
    if(!objectName) \
    { \
          objectName = [[objectType alloc] init]; \
    } \
    return objectName; \
} \
\
- (void)set##objectName:(objectType *)value \
{ \
    [value retain]; \
    [objectName release]; \
    objectName = value; \
}

? , , , , /. . , - !;)


. Warning: Untested Code Ahead:

// ....
@interface SomeClass : NSObject {
    NSObject *someObj;
}
@end

@implementation SomeClass
// ....
SYNTHESIZE_LAZY_INITIALIZER_FOR_OBJECT(someObj, NSObject);
// ....
@end
+5

@synthesize Objective-C #, getters/seters. , , .

#define, . , .

+2

All Articles