1

I am using Xcode 4.4.1. When I define @property like UINavigationController or NSArrayin .h file I have to @synthesize it in .m file. But some @property like UITabBarController or NSString I don't have to @synthesize it to make it work.

My Question is what @propertys need to @synthesize and what need not to.

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UITabBarController *_tabBar;
UINavigationController *_navBar;
}

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) Employee *empView;
@property (nonatomic, retain) UITabBarController *_tabBar;
@property (nonatomic, retain) UINavigationController *_navBar;

AppDelegate.m

@implementation AppDelegate
@synthesize _navBar;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
Employee *emp = [[Employee alloc]initWithNibName:@"Employee" bundle:nil];
self._tabBar = [[UITabBarController alloc]init];
self._navBar = [[UINavigationController alloc]initWithRootViewController:emp];
self._tabBar.viewControllers = [NSArray arrayWithObjects:_navBar, nil];
self.window.rootViewController = self._tabBar;
self._navBar.navigationBar.tintColor = [UIColor brownColor];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

When I @synthesize UINavigationController I get UINavigationController and UITabBarController. But when I don't @synthesize UINavigationController I don't get UINavigationController but UITabBarController is displayed.

In both cases I didn't @synthesize UITabBarController

Thanks

2

1 Answer 1

13

Since the last version of the compiler (LLVM) shipped with Xcode 4.4, the @synthesize directive is not needed anymore.

Every @property you declare for which you don't use @synthesizeexplicitly will have its accessor synthesized automatically as if you wrote @synthesize yourprop = _yourprop;. That's a new feature of the latest compiler (as before you had to write @synthesize (or implement the accessors) for all your @properties explicitly).

Note that of course you can still use the @synthesize property explicitly if you prefer to (just like old times). This can be a way to explicitly design the backing instance variable to use for the property. But as a matter of fact I strongly recommend to forget about instance variables (in fact that's ages I don't use explicit instance variables between the curly braces of my @interface declaration anymore), and only work with @property declarations. With that and the new feature that let the compiler generate the @synthesize directives for you, you will avoid a lot of glue code and make your classes more compact to write.


FYI you can toggle a warning when you have a @property for which is implicitly synthesized (meaning for which you didn't write the @synthesize directive explicitly thus for which the compiler now synthesize it for you). Simply go to the Build Settings of your project and turn on the "Implicit Synthesized Properties" warning (under "Apple LLVM compiler 4.0 - Warnings - Objective-C" section), and the compiler will tell you about all the properties for which it implicitly synthesize the accessors as you didn't mention the @synthesize directive yourself.

6
  • Please read my answer again. (1) Not explicitly @synthesize makes the compiler generate a @synthesize propname = _propname, so in your cas it generates @synthesize _navBar = __navBar, thus generates an ivar named __navBar that is different from your _navBar ivar. This explains why self._navBar is not nil and neither is __navBar, but the _navBar ivar is nil. But you use it directly in the self._tabBar.viewControllers = [NSArray arrayWithObjects:_navBar, nil]; line (instead of using the property or the associated __navBar ivar). Sep 16, 2012 at 17:53
  • (2) so as I stated in my answer, I strongly recommend to stop declaring ivars (that was never necessary since the first release of iOS, by the way, as @synthesize has always generated ivars when needed) and thus remove the UINavigationController *_navBar; declaration in your @interface method (you will understand your mistake right away then), and moreover I also strongly recommand to avoid prefixing your @property names with an underscore (as that adds some confusion between the property and its backing instance variable) Sep 16, 2012 at 17:56
  • Final note, if all the above was not clear enough: writing @synthesize _navBar explicitly is the same as writing @synthesize _navBar = _navBar, namely the property _navBar uses the ivar _navBar (same name as the prop) as its backing store. Whereas when you don't write the @synthesize property is the same as writing @synthesize _navBar = __navBar, namely the name of the generated ivar (used as a backing store for the property) is the name of the property preceded by an underscore. Thus your property is then not associated with the _navBar ivar but with the __navBar ivar. Sep 16, 2012 at 18:00
  • Thanks a lot. Now things are making sense for me. :-)
    – Ankur Arya
    Sep 16, 2012 at 18:04
  • can you please tell me why it is not the same in the case of UITabbarController.
    – Ankur Arya
    Sep 16, 2012 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.