3

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

For example:

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

@implementation
@lazyInitialize object;
@end

And then somehow define @lazyInitialize to generate a lazy initializer method

//@lazyInitialize

- (id)"property name"
{
   if (!"property name")
   {
      "property name" = [[["property name" class] alloc] init];
   }
   return "property name";
}
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • Not exactly a solution, but probably of interst: http://stackoverflow.com/questions/1160498/synthesize-vs-dynamic-what-are-the-differences – SteAp May 01 '11 at 16:45
  • 3
    Might this type of statement be of help? @property (copy,setter=setDefaultTitle:) NSString* title; – SteAp May 01 '11 at 16:47
  • Stefan that seems really interesting. Can setDefaultTitle be a class method in another Class? – aryaxt May 01 '11 at 17:02
  • No, I don't think so. I suppose, you need to provide a method signature of your own class. – SteAp May 01 '11 at 17:04

2 Answers2

5

You could try something different, though. I wouldn't have thought of this more than a couple days ago, but I happened to be reading Cocoa With Love. In the post linked, he discussed how he made a #define macro that would "generate" the entire class for a singleton into wherever you called the macro from. You can download his code for this (may give ideas on your own implementation).

Perhaps something like (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; \
}

would work? I apologize that I don't have time to properly test it for you, so take that as fair warning that this isn't a quick copy/paste solution. Sorry about that. Hopefully it is still useful! ;)


Example Usage

This should work, again Warning: Untested Code Ahead:

Header

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

Implementation

@implementation SomeClass
// ....
SYNTHESIZE_LAZY_INITIALIZER_FOR_OBJECT(someObj, NSObject);
// ....
@end
Ryan Wersal
  • 3,210
  • 1
  • 20
  • 29
  • Interesting, but how can I use this? – aryaxt May 01 '11 at 18:41
  • @Rayan, pure Objective-C looks way better. Sure, there is no pure way to define a generic getter or setter, but C-macrosses (at my taste) are worse than nothing. Once I got a project with such code for support and refactoring - removing such macrosses was the best refactoring ever :) The code became readable. – Gobra May 01 '11 at 18:54
  • Why are there back-slashes in there? What do they represent? – aryaxt May 01 '11 at 19:02
  • @aryaxt: They escape newlines to make them part of the `#define`. Without the backslashes, the first newline would be seen as the end of the single-line `#define` statement. – BoltClock May 01 '11 at 19:04
  • 1
    @Gobra Yeah, I would concur. That is actually why I *don't* use the code from that blog. I would rather deal with writing the code each time and having a template to work from rather than use this. I proposed it as a possible alternative to the standard "no" answer, not necessarily as the de facto answer to this question. Personally, I would rather just have a snippet of code that I copy/paste when needed than do this as my solution. – Ryan Wersal May 01 '11 at 19:19
  • @Ryan Can you explain how I can use the code you provided? where do I write the code? I defined it in my prefic.pch and I try something like this in my viewDidLoad of a viewController. SYNTHESIZE_LAZY_INITIALIZER_FOR_OBJECT ("WebServiceClient", "webService");, and I get the following error "expected expression before ')' token. – aryaxt May 01 '11 at 19:26
  • Thanks for the answer. This is really interesting, but my purpose of creating a custom @synthesize was to make my code cleaner, and unfortunately this is not clean at all :(. Anyways great answer. – aryaxt May 01 '11 at 20:36
  • @aryaxt Judging from the comments, everyone else agrees with you (including myself), so definitely no hard feelings. Sorry it isn't more applicable to your situation (or any for that matter!). – Ryan Wersal May 01 '11 at 21:18
2

@synthesize in Objective-C works similarly to automatic property syntax in C#, in that both generate the minimal required syntax for creating property getters/setters. In both languages, if you want custom functionality, you need to implement them manually.

I really like Ryan Wersal's answer of using a #define to roll your own macro. You'd still have to write the method yourself, but you only do it once.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356