When declaring class properties/variables, can you just declare it via @property?
I've noticed that some generated classes only declare class properties/variables via @property, and don't include them within the @interface, as such:
@interface AddItemViewController : UITableViewController {
}
@property (nonatomic, retain) UITextField *itemName;
I was just curious if that's an acceptable way to do it, or if that is done for different reasons?
I normally do this:
@interface AddItemViewController : UITableViewController {
UITextField *itemName;
}
@property (nonatomic, retain) UITextField *itemName;
I declare it first in the @interface and then add the @property for it...
* Update *
I just wanted to update this a bit, because it's still not 100% c开发者_开发技巧lear to me.
I always thought that to declare a @property, you first needed to declare it within the @interface first, and then I saw this:
@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
All of those @property declarations are declared only as @properties, and not within the @interface.
For example, if I had say NSString *myString
- I can declare that in the @interface and not as a @property and still have access to it no problem, but the getters and setters won't be created. I could also declare it in both. But what if I just declare it as @property, as such:
@interface AddItemViewController : UITableViewController {
}
@property (nonatomic, retain) NSString *myString;
Notice how I didn't add it between the @interface { } - how does it differ.
Sorry for repeating, but I'm just trying to reword this so that I can get an answer that makes more sense to me.
With the "modern" runtime, which the iPhone uses, the compilers can create the instance variable for you. Just use:
@synthesize itemName;
or if you prefer...
@synthesize itemName=_itemName;
...in your implementation. The compilers will then create ivar 'itemName' or '_itemName'.
This is of course for the case that the property is a simple getter/setter for one particular instance variable.
EDIT: NVM, per @bbum, what I thought of in my mind as the "32-bit" sim is actually the older simulator that didn't behave like the new runtime. The newer simulator is still 32-bit, and supports this behavior. See his comment below.
update
In response to your updated question:
The "interface" for a class is everything up to the @end. I think what you are calling "interface" is actually just the instance variables within the {}. What is between the {} are the instance variables for your class. The whole @interface includes those instance variables PLUS the method and @property declarations between the {} and the @end.
So I think what you are really asking is if you have a @property in your @interface, and that @property is just a simple getter/setter pair, then do you need to declare a "backing" instance variable also in your @interface, within the {}.
The answer for iPhone is NO. The compilers (both) can create that instance variable for you.
I hope that answers the question?
It is perfectly acceptable to do it this way. You would however need to implement the setter/getter methods yourself. These can not be created using the @synthesize syntax.
One reason to use this approach could be to have the properties based on something more complex than just setting and getting a value. It doesn't however make much sense for simple Nib connections as in your example.
精彩评论