我们在xcode 4.4.1中合成了哪些属性

Ank*_*rya 1 ios xcode4.4

我正在使用Xcode 4.4.1.当我在.m文件中定义 @property like UINavigationControllerNSArrayin .h文件时,我必须@synthesize在.m文件中.但有些人@property喜欢UITabBarController或者NSString我没有这样做才能@synthesize让它发挥作用.

我的问题是@property需要@synthesize什么,什么不需要.

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;
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

@synthesize UINavigationController我得到UINavigationControllerUITabBarController.但是当我不这样做时,我 @synthesize UINavigationController不会得到UINavigationController但是UITabBarController会显示出来.

在这两种情况下我都没有 @synthesize UITabBarController

谢谢

Ali*_*are 13

自Xcode 4.4附带的最新版本的编译器(LLVM)以来,@synthesize不再需要该指令.

@property您声明未@synthesize明确使用的每一个都将自动合成其访问器,就像您编写的那样@synthesize yourprop = _yourprop;.这是最新编译器的一个新功能(就像你必须@synthesize为所有@properties显式编写(或实现访问器)之前).

请注意,当然,@synthesize如果您愿意,您仍然可以明确地使用该属性(就像过去一样).这可以是显式设计用于属性的支持实例变量的方法.但事实上我强烈建议忘记实例变量(事实上​​,我不再使用声明的花括号之间的显式实例变量@interface),并且只能使用@property声明.有了这个以及让编译器@synthesize为您生成指令的新功能,您将避免使用大量的粘合代码并使您的类更紧凑.


FYI你可以在你有一个隐式合成的时候切换一个警告@property(这意味着你没有@synthesize明确地编写指令,因此编译器现在为你合成它).只需转到项目的Build Settings并打开"Implicit Synthesized Properties"警告(在"Apple LLVM编译器4.0 - 警告 - Objective-C"部分下),编译器将告诉您隐藏的所有属性合成访问器,因为你自己没有提到@synthesize指令.