问题描述
我正在使用Xcode 4.4.1。当我定义 @property
,如 UINavigationController
或 NSArray
in。 h文件我必须在.m文件中 @synthesize
。但是一些 @property
喜欢 UITabBarController
或 NSString
我不知道它必须 @synthesize
才能使它工作。
I am using Xcode 4.4.1. When I define @property
like UINavigationController
or NSArray
in .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.
我的问题是什么 @ property
需要 @synthesize
以及什么不需要。
My Question is what @property
s need to @synthesize
and what need not to.
AppDelegate.h
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
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
。但是,当我不 @合成
的UINavigationController
我不明白的UINavigationController
但显示 UITabBarController
。
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.
在这两种情况下我都没有 @synthesize
UITabBarController
In both cases I didn't @synthesize
UITabBarController
谢谢
推荐答案
自上一版本的编译器( LLVM 4.4附带的LLVM),不再需要 @synthesize
指令。
Since the last version of the compiler (LLVM) shipped with Xcode 4.4, the @synthesize
directive is not needed anymore.
每 @property
你声明你不使用 @synthesize
显式地自动合成其访问器,就好像你写了 @synthesize yourprop = _yourprop;
。这是最新编译器的一个新功能(就像你必须为所有 @properties $编写
@synthesize
(或实现访问器)之前一样c $ c>明确地表示。
Every @property
you declare for which you don't use @synthesize
explicitly 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).
注意,当然你仍然可以使用 @synthesize
属性明确的,如果你愿意(就像过去一样)。这可以是显式设计用于属性的后备实例变量的方法。但事实上我强烈建议忘记实例变量(实际上我的年龄我不会在我的 @interface的大括号之间使用显式实例变量
声明了,只能使用 @property
声明。有了这个以及让编译器为你生成 @synthesize
指令的新功能,你将避免使用大量的粘合代码并使你的类更紧凑。
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.
当你有切换警告 > @property 是隐式合成的(意思是你没有明确写出 @synthesize
指令,因此编译器现在合成它为你)。只需转到项目的Build Settings并打开Implicit Synthesized Properties警告(在Apple LLVM编译器4.0 - 警告 - Objective-C部分下),编译器将告诉您隐式的所有属性合成访问器,因为你自己没有提到 @synthesize
指令。
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.
这篇关于我们在xcode 4.4.1中合成了哪些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!