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
Since the last version of the compiler (LLVM) shipped with Xcode 4.4, the
@synthesizedirective is not needed anymore.Every
@propertyyou 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@propertiesexplicitly).Note that of course you can still use the
@synthesizeproperty 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@interfacedeclaration anymore), and only work with@propertydeclarations. With that and the new feature that let the compiler generate the@synthesizedirectives 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
@propertyfor which is implicitly synthesized (meaning for which you didn’t write the@synthesizedirective 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@synthesizedirective yourself.