您的当前位置:首页正文

iOS中@Synthesized现在还有啥用

来源:华佗小知识

Most Properties Are Backed by Instance Variables

You Can Customize Synthesized Instance Variable Names

As mentioned earlier, the default behavior for a writeable property is to use an instance variable called _propertyName.

If you wish to use a different name for the instance variable, you need to direct the compiler to synthesize the variable using the following syntax in your implementation:

@implementation YourClass
@synthesize propertyName = instanceVariableName;
...
@end

For example:

@synthesize firstName = ivar_firstName;

In this case, the property will still be called firstName, and be accessible through firstName and setFirstName: accessor methods or dot syntax, but it will be backed by an instance variable called ivar_firstName.

Important: If you use @synthesize without specifying an instance variable name, like this:

@synthesize firstName;
  • the instance variable will bear the same name as the property.
  • In this example, the instance variable will also be called firstName, without an underscore.

References