iPhone - Apple 默认示例代码缺少一些变量?
在 XCode 4 中,当您创建一个新的 View-base-application 项目时,这里是 AppDelegate 的 .h :
#import <UIKit/UIKit.h>
@class TestAppleProjectViewController;
@interface TestAppleProjectAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet TestAppleProjectViewController *viewController;
@end
以及 .m 上的一些项目:
@implementation TestAppleProjectAppDelegate
@synthesize window=_window;
@synthesize viewController=_viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
我可以看到没有变量的属性...
我可以看到使用类中不存在的 var 名称进行综合...
我可以看到对这些属性的调用...
但是...变量在哪里?
为什么这样的代码有效?
是否不再需要在类中定义变量?属性够吗?
In XCode 4, when you create a new View-base-application project, here is the .h of the AppDelegate :
#import <UIKit/UIKit.h>
@class TestAppleProjectViewController;
@interface TestAppleProjectAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet TestAppleProjectViewController *viewController;
@end
And some items on the .m :
@implementation TestAppleProjectAppDelegate
@synthesize window=_window;
@synthesize viewController=_viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I can see properties without vars...
I can see synthesize with var names that does not exist in the class...
I can see calls to those properties....
But... Where are the vars ?
Why such a code is working ?
Are there no more need to define vars into the class ? Are properties enough ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,当您将 var 声明为属性时,不必再次将其声明为 var。之前我们必须再次声明的行为是 Xcode 早期版本中的一个错误。现在它已得到纠正。
这只是一个习惯,减少重复的代码就很好了。 (默认情况下,它会创建一个以“_”开头的同名隐藏变量)。
In fact when you declare a var as a property, you don't have to declare it again as var. The previous comportment where we had to declare it again was a bug in the previous versions of Xcode. And now it's corrected.
It's just an habit to take, less code duplicate and it's good. (By default it's creating ah hidden var with the same name beginning by "_").
我对此不确定......我认为当您编写
@synthesize prop=_var
时,它会自动为属性 prop 创建一个名为 _var 的私有实例变量。I'm not sure about this... I think when you write
@synthesize prop=_var
it will automatically create a private instance variable named _var for the property prop.