With properties in obj-c do we need to declare instance variables?

With properties in obj-c do we need to declare instance variables?

For example, if my .h file looks like this:

@interface MyClass : NSObject { } @property (nonatomic, retain) NSNumber *someId; @property (nonatomic, retain) NSDate *expires; @property (nonatomic, retain) NSString *someString; ... 

It's good? It compiles and works, is that the "perfect thing"? Or will I really do the following:

 @interface MyClass : NSObject { NSNumber *someId; NSDate *expires; NSString *someString; } @property (nonatomic, retain) NSNumber *someId; @property (nonatomic, retain) NSDate *expires; @property (nonatomic, retain) NSString *someString; ... 

Is there any difference in any of the above methods if I plan to always use property accessors?

Does @synthesize help instantiate instances for me?

+4
source share
2 answers

You got it right, @synthesize takes care of creating ivars for you. (This is a new runtime feature compatible with 64-bit Macs and iOS devices.)

What is connected with this is that you can @property in your class extension and completely hide your ivars from .h, completely separating the interface from the implementation.

You can also completely remove "{}" from .h, making it really clean.

+5
source

This is found in apple docs:

With a modern runtime, if you do not provide an instance variable, the compiler adds it for you

+1
source

All Articles