我们在xcode 4.4.1中综合了哪些属性
我正在使用Xcode 4.4.1。 当我在.h文件中定义@property
像UINavigationController
或NSArray
,我必须在.m文件中将它合成为。 但是像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;
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; }
当我@synthesize
UINavigationController
我得到UINavigationController
和UITabBarController
。 但是,当我不@synthesize
UINavigationController
我没有得到UINavigationController
但显示UITabBarController
。
在这两种情况下,我没有@synthesize
UITabBarController
谢谢
由于Xcode 4.4附带的最新版本的编译器(LLVM),不再需要@synthesize
指令。
你声明的每个@property
你不明确使用@synthesize
会自动@synthesize yourprop = _yourprop;
它的访问器,就像你写了@synthesize yourprop = _yourprop;
。 这是最新编译器的一个新function(因为之前你必须明确地为你所有的@properties
编写@synthesize
(或实现访问器))。
请注意,如果您愿意(当然也可以像以前一样),仍然可以使用@synthesize
属性。 这可以是显式devise用于属性的后台实例variables的一种方式。 但是事实上, 我强烈build议不要使用实例variables (实际上这个年龄段我不再使用@interface
声明的大括号之间的显式实例variables),只能使用@property
声明。 有了这个新特性,编译器就可以为你生成@synthesize
指令,这样你就可以避免大量的胶水代码,并且使你的类更加紧凑。
仅当您拥有隐式合成的@property
(意思是您没有明确编写@synthesize
指令,编译器现在为您合成它)时,您可以切换警告 。 只需转到项目的“生成设置”,然后打开“隐式合成属性”警告(在“Apple LLVM编译器4.0 – 警告 – Objective-C”部分下),编译器会告诉您隐含的所有属性综合访问器,因为你自己没有提到@synthesize
指令。