So, briefly, at sign Synthesize

At sign synthesize is the key word that the compiler automatically generates an implementation of setter/getter methods (implementation implementation implementation is important). When we use @property in our current development, it automatically generates the @synthesize property name = _ property name in.m, instead of the @property itself generating setter/getter methods, @Property itself is just a declaration that generates setter/getter methods (declaration declaration is important).

The @synthesize might write something like this:

  1. @synthesize boy;

  2. @synthesize boy = boy;

  3. @synthesize boy = _boy;

  4. (Girl is a pre-declared variable)

 @implementation ViewController {
     NSObject *girl;
 }
 @synthesize boy = girl;
Copy the code
  1. @synthesize boy = noDeclaredVar;(noDeclaredVar is typed without a declaration)

All five of the above codes work fine. It’s a little convoluted, and beginners may not understand what this is all about.

So just to make it clear, at sign synthesize boy by default is equal to at sign synthesize boy = boy; . So, just understand one way: @synthesize xx = yy; Can. This “equation” says “I used @property (nonatomic, strong) NSObject *boy; The actual instance variable used in setter/getter methods for this declared “property” is YY, which is automatically declared as an instance variable if yy is not declared manually.

Add: xx must be declared with @property.

Let’s look at the five keyword codes above.

  1. in@propertyDeclared boy, used in setter/getter methodsNSObject *boy;The instance variable to assign and return.
  2. With 1.
  3. in@propertyDeclared boy, used in setter/getter methodsNSObject * _boy;The instance variable to assign and return.
  4. in@propertyDeclared boy, used in setter/getter methodsNSObject *girl;The instance variable to assign and return.
  5. in@propertyDeclared boy, used in setter/getter methodsNSObject * noDeclaredVar;The instance variable to assign and return.

At sign synthesize uses the specified property

@interface ViewController () @property (nonatomic, copy) NSObject *boy; @end @implementation ViewController { NSObject *boy; NSObject *girl; } @synthesize boy = girl; - (void)viewDidLoad { [super viewDidLoad]; self.boy = [[NSObject alloc] init]; NSLog(@" Which baby has been born? boy : %@ girl : %@",boy, girl); } @endCopy the code
  1. Is the member property NSObject *boy declared in the @implementation declaration the same object as boy declared in the @Property declaration? Does the _boy instance variable still exist?

    No, the @synthesize already specifies girl as the instance variable to use in setter/getter methods, so it no longer automatically generates the _boy instance variable.

  2. @synthesize boy = girl; Did you change a boy into a girl or a girl into a boy?

    It’s not changing because there’s no _boy anymore. SetBoy – (void)setBoy:(NSObject *)boy is implemented as girl = boy, so _boy is replaced by girl. The _boy member variable no longer exists.