When declaring class properties / variables, can you just declare it via @property?

I noticed that some generated classes declare class properties / variables via @property and do not include them in @interface, as such:

@interface AddItemViewController : UITableViewController { } @property (nonatomic, retain) UITextField *itemName; 

I was just wondering if this is an acceptable way to do this, or if this is done for various reasons?

I usually do this:

 @interface AddItemViewController : UITableViewController { UITextField *itemName; } @property (nonatomic, retain) UITextField *itemName; 

I declare it first in @interface and then add @property for it ...

* Update *

I just wanted to update this a bit, because it is still not 100% clear to me.

I always thought that to declare @property first you need to declare it first in the @interface interface, 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 these @property declarations are only declared as @properties, and not inside @interface.

For example, if I said NSString *myString - I can declare that in @interface, and not like @property, and still it does not have access to it, but creators and setters will not be created. I could also declare this in both. But what if I simply declare it as @property, as such:

 @interface AddItemViewController : UITableViewController { } @property (nonatomic, retain) NSString *myString; 

Note that I did not add it between @interface {} - how it differs.

Sorry for repeating, but I'm just trying to change this to get an answer that makes more sense to me.

+4
source share
2 answers

Using the “modern” runtime that iPhone uses, compilers can create an instance variable for you. Just use:

 @synthesize itemName; 

or if you prefer ...

 @synthesize itemName=_itemName; 

... in your implementation. Compilers will then create ivar 'itemName' or '_itemName'.

This, of course, applies when the property is a simple getter / setter for one particular instance variable.

EDIT: NVM, per @bbum, which I thought about, since the “32-bit” sim is actually a senior simulator that didn't behave like a new runtime. The new simulator is still 32-bit and supports this behavior. See His comment below.

Update

In response to your updated question:

The "interface" for the class is everything up to @end. I think what you call an “interface” is really just instance variables in {}. Between {} there are instance variables for your class. The entire @interface includes these instance variables of the PLUS method and the @property declarations between {} and @end.

So, I think that you are really asking if you have @property in your @interface and that @property is just a simple getter / setter pair, then you need to declare the "backing" instance variable also in your @interface, inside {} .

The answer for the iPhone is NO. Compilers (both) can create this instance variable for you.

Hope he answers the question?

+8
source

It is perfectly acceptable to do so. You, however, will need to implement the setter / getter methods yourself. They cannot be created using the @synthesize syntax.

One reason for using this approach may be the presence of properties based on something more complex than just setting and getting the value. However, this does not make much sense for simple Nib connections, as in your example.

0
source

Source: https://habr.com/ru/post/1346462/


All Articles