This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

@synthesize

Synthesize means to synthesize, and it does two things:

  1. Generate setter/getter methods for properties
  2. Give attributes individual names

But normally, we don’t use the synthesize, because when we define the property, we use at sign roperty, and this will automatically synthesize it for us.

@property (nonamic,copy) NSString *name; // synthesize @synthesize name = _name;Copy the code

So when you use property, you just have getter/setter methods, and you can go right through the slide line

So when do we synthesize?

  1. You need to give attributes individual names
  2. Setter /getter methods are added manually
  3. Implemented withpeopertyProperties of theprotocol

Give attributes individual names

@interface Persion () @property (nonatomic, copy) NSString *hobby; @end@implementation // Synthesize a name for a property @synthesHobby = _newHobby; - (instancetype)init { if (self = [super init]) { self->_newHobby = @"hello"; self.hobby = @"hello2"; NSLog(@"hobby:%@ _newHobby:%@",self.hobby, self->_newHobby); } return self; } @endCopy the code

Hobby: HELLO2_newhobby :hello2

In the example above, @synthesize Hobby = _newHobby; Alias set

We then verify that alias _newHobby and self.hobby are the same property by assigning and then evaluating the alias

Setter/getter methods are added manually

After you manually add setter/getter methods, the system will not automatically create member variables. If you need to use member variables, you must manually add @synthesize to generate member variables.

Examples are as follows:

After you implement the setter and getter for the hobby property, you cannot assign to the _hobby member variable.

When the @synthesize is used, the corresponding member variable is called.

It is worth noting:

If the property is readable and writable, but we override only one of the setter and getter methods, the system automatically synthesizes the remaining methods and member variables

Implemented withpeopertyProperties of theprotocol

For example, there is an agreement:

@protocol PersionProtocol <NSObject>

@property (nonatomic, copy) NSString *name;

@end
Copy the code

Student then keeps the agreement

@interface Student : Persion<PersionProtocol>
@property (nonatomic, copy) NSString *teacher;
@end
Copy the code

Then in the student.m file an error is reported if a member variable is used

And then at sign synthesize, that’s it

@dynamic

So what does dynamic mean? Looking through my old English-Chinese dictionary, dynamic means “dynamic”

What does that mean in iOS, it means that the setter and getter for this property doesn’t have to be generated for me by the system.

So let’s verify that if we use @dyanmic we’re not actually generating getter and setter methods

PS: I am not free

See, when we use @dynamic, we can’t find setHobby when we use dot assignment:

A little question came to mind

Does the system automatically generate underlined member variables when using @dynamic?

Let’s verify:

You can see that with @dynamic, underlined member variables are not created either