0

Let's say i have a class definition header file like this :

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) SomeObject *managedObject;

@end

instead of defining the @synthesize on managedObject to create the getters/setters a friend of mine told me i can do the following header definition using a class extension to do the synthesis more cleanly:

#import "TSPAppDelegate.h"

@interface TSPAppDelegate () //notice the class extension here

@property (strong, nonatomic) SomeObject *managedObject; //this will already be synthesized since its an extension


@end

Could some one explain how this works using the extensions ?

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • You don't need to use @synthesize any more, it's done for you automatically. There's no difference between putting a property in the .h vs. an extension as far as synthesis of the properties goes. – rdelmar Mar 19 '15 at 23:59
  • As noted, `@synthesize` is no longer necessary. What differs in putting the `@property` is the scope of the property: putting it in the class interface header makes it visible to every class importing this header file (like the `public` keyword in Java), while putting it in the class extension makes it visible only to that file (usually the `.m` file). – neilvillareal Mar 20 '15 at 00:33

1 Answers1

-2

I think your friend is incorrect. You have to @synthesize to have the getters/setters implemented for you

Choppin Broccoli
  • 3,048
  • 2
  • 21
  • 28
  • 1
    Getters and setters are created automatically for you , there's no need to use @synthesize any more unless you're overriding both the setter and getter (or getter only for a read-only property). – rdelmar Mar 19 '15 at 23:57