What properties do we synthesize in xcode 4.4.1

青春壹個敷衍的年華 提交于 2019-12-21 06:37:48

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/12449005/what-properties-do-we-synthesize-in-xcode-4-4-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!