Synthesize By Default

In 2012 Apple introduced Synthesize By Default in Xcode. You no longer have to synthesize instance variables. Just declare them as you normally would in the .h file and the compiler will automatically synthesize them for you. I learned how to use Xcode before the transition so I just learned about this by accident. If you haven’t done so, watch these two videos from WWDC 2012: Session 405 – Modern Objective-C and Session 413 – Migrating to Modern Objective-C.

Synthesize By Default is not available for NSManagedObjects so if your header file contains something like this:


@interface WordList : NSManagedObject

You’ll have to synthesize.

You can still manually create setters or getters and the compiler will automatically create the other accessor.

One caveat. I stopped supporting iOS4.3 and still have code that is compatible with iOS4. Since doesn’t have the weak indicator, you need to change assign to weak, then delete the ‘__unsafe_unretained id delegate;’ line. Then you can delete the @synthesize. If you still want to support iOS4, you’ll need to synthesize these variables.

Before


@implementation PracticeSightWords
@synthesize delegate = _delegate;


@interface PracticeSightWords : UIViewController {
    
    __unsafe_unretained id <PracticeSightWordsDelegate> delegate;
}

@property (nonatomic, assign) id <PracticeSightWordsDelegate> delegate;

After


@interface PracticeSightWords : UIViewController 

@property (nonatomic, weak) id <PracticeSightWordsDelegate> delegate;

I have my compiler warnings Turned up to 11 so I needed to turn off the warnings for “Implicit Synthesized Properties” in the ‘Warnings-Objective C’ section of the build settings.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.