@interface MyObject : NSObject { NSString *string; } @property ()NSString *string; @end @implementation MyObject @synthesize string; //@synthesize string = _string; /* - (void)setString:(NSString *)string { _string = string; } - (NSString *)string { return _string; } * /Copy the code

Conclusion 1: The @synthesize modifier combines the member variables of a string and the implementation of the getter and setter methods of string.

First of all, without getting into compiler issues, the @property declaration property in Xcode today automatically generates a member variable with an _ and the property declaration and implementation of getter and setter methods (provided you don’t rewrite the implementation of both setter and getter methods, Because overwriting both methods at the same time does not generate member variables with _.

Now, in older versions of Xcode, we have to declare a member variable, the @property modified property, the @synthesize modified property, associate the @property modified property with the declared member variable, At the same time, synthesize the implementation of the setter and getter methods for this property.

Application: Today, you can still use @synthesize to modify properties. When you write a protocol, you use an @property modifier in the protocol, which isn’t very common, but it’s possible, when an object complies with this protocol, The compiler will prompt you that the properties you declared in the protocol need to be decorated with @synthesize to generate member variables and implementations of setter and getter methods.

Conclusion 2: The @dynamic modifier is used to write its own setter and implementation of setter methods, avoiding the editor’s ⚠️.

Application: When you declare a property in a category, the property does not generate a member variable with an _ and does not synthesize the implementation of getter and setter methods. It only declares the property and getter and setter methods. Now, what you need to do is decorate this property with @dynamic in the @implementation, telling the compiler that you’re going to write your own implementation of the setter and getter for this property. (The method of the property that this modifier modifies must have been added at runtime, because the category itself is added at runtime.)