【发布时间】:2011-04-03 18:18:43
【问题描述】:
我注意到一些生成的类只通过@property 声明类属性/变量,并没有将它们包含在@interface 中,例如:
@interface AddItemViewController : UITableViewController {
}
@property (nonatomic, retain) UITextField *itemName;
我只是好奇这是一种可以接受的方式,还是出于不同的原因?
我通常这样做:
@interface AddItemViewController : UITableViewController {
UITextField *itemName;
}
@property (nonatomic, retain) UITextField *itemName;
我先在@interface中声明它,然后为它添加@property...
* 更新 *
我只是想稍微更新一下,因为我还不是 100% 清楚。
我一直认为要声明一个@property,首先需要在@interface中声明它,然后我看到了这个:
@interface mInventoryAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
所有这些@property 声明都只声明为@properties,而不是在@interface 中。
例如,如果我说NSString *myString - 我可以在@interface 中声明它而不是@property 并且仍然可以访问它没有问题,但不会创建getter 和setter。我也可以在两者中声明它。但是,如果我只是将其声明为@property 会怎样:
@interface AddItemViewController : UITableViewController {
}
@property (nonatomic, retain) NSString *myString;
注意我没有在 @interface { } 之间添加它 - 它有什么不同。
很抱歉重复一遍,但我只是想改写一下,以便我能得到一个对我更有意义的答案。
【问题讨论】:
标签: iphone objective-c