这个2 @synthesize模式与推荐的模式有什么不同?

时间:2022-09-06 13:25:44

many Places in the sample code i have seen 2 different way of @synthesize variable. For Example here i am taking 1 sample button. @property (strong, nonatomic) IBOutlet UIButton *logonButton;

在示例代码中的许多地方,我看到了@synthesize变量的两种不同方式。例如,这里我取一个样本按钮。@property(强,非原子)IBOutlet UIButton *logonButton;

1.@synthesize logonButton = _logonButton;

1。@ synthesize logonButton = _logonButton;

2.@synthesize logonButton;

2。@ synthesize logonButton;

among this 2 methods which one is recommended?

在这两种方法中,推荐哪一种?

2 个解决方案

#1


7  

Short Answer

简短的回答

The first method is preferred.

第一种方法是首选。

Long Answer

长回答

The first example is declaring that the generated ivar for the logonButton property should be _logonButton instead of the default generated ivar which would have the same name as the property (logonButton).

第一个示例是声明为logonButton属性生成的ivar应该是_logonButton,而不是默认生成的ivar,它将具有与属性相同的名称(logonButton)。

The purpose of this is to help prevent memory issues. It's possible that you would accidentally assign an object to your ivar instead of your property, and it would then not be retained which could potentially lead to your application crashing.

这样做的目的是为了防止记忆问题。您可能会意外地将一个对象分配给您的ivar而不是您的属性,并且它将不会被保留,这可能会导致您的应用程序崩溃。

Example

例子

@synthesize logonButton;

-(void)doSomething {
    // Because we use 'self.logonButton', the object is retained.
    self.logonButton = [UIButton buttonWithType:UIButtonTypeCustom];


    // Here, we don't use 'self.logonButton', and we are using a convenience
    // constructor 'buttonWithType:' instead of alloc/init, so it is not retained.
    // Attempting to use this variable in the future will almost invariably lead 
    // to a crash.
    logonButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
}

#2


2  

  1. Means that the automatically generated set/get methods for the property are using an ivar with a different name - _logonButton.

    意味着为属性自动生成的set/get方法正在使用具有不同名称的ivar - _logonButton。

    -(void)setLogonButton:(UIButton)btn {
    _logonButton = [btn retain]; // or whatever the current implementation is
    }

    -(void)setLogonButton:(UIButton)btn {_logonButton = [btn retain];//或任何当前实现是}

  2. Means that the automatically generated set/get methods for the property are using an ivar with the same name logonButton.

    意味着该属性的自动生成的set/get方法使用的是一个具有相同名称logonButton的ivar。

    -(void)setLogonButton:(UIButton)btn {
    logonButton = [btn retain]; // or whatever the current implementation is
    }

    -(void)setLogonButton:(UIButton)btn {logonButton = [btn retain];//或任何当前实现是}

#1


7  

Short Answer

简短的回答

The first method is preferred.

第一种方法是首选。

Long Answer

长回答

The first example is declaring that the generated ivar for the logonButton property should be _logonButton instead of the default generated ivar which would have the same name as the property (logonButton).

第一个示例是声明为logonButton属性生成的ivar应该是_logonButton,而不是默认生成的ivar,它将具有与属性相同的名称(logonButton)。

The purpose of this is to help prevent memory issues. It's possible that you would accidentally assign an object to your ivar instead of your property, and it would then not be retained which could potentially lead to your application crashing.

这样做的目的是为了防止记忆问题。您可能会意外地将一个对象分配给您的ivar而不是您的属性,并且它将不会被保留,这可能会导致您的应用程序崩溃。

Example

例子

@synthesize logonButton;

-(void)doSomething {
    // Because we use 'self.logonButton', the object is retained.
    self.logonButton = [UIButton buttonWithType:UIButtonTypeCustom];


    // Here, we don't use 'self.logonButton', and we are using a convenience
    // constructor 'buttonWithType:' instead of alloc/init, so it is not retained.
    // Attempting to use this variable in the future will almost invariably lead 
    // to a crash.
    logonButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
}

#2


2  

  1. Means that the automatically generated set/get methods for the property are using an ivar with a different name - _logonButton.

    意味着为属性自动生成的set/get方法正在使用具有不同名称的ivar - _logonButton。

    -(void)setLogonButton:(UIButton)btn {
    _logonButton = [btn retain]; // or whatever the current implementation is
    }

    -(void)setLogonButton:(UIButton)btn {_logonButton = [btn retain];//或任何当前实现是}

  2. Means that the automatically generated set/get methods for the property are using an ivar with the same name logonButton.

    意味着该属性的自动生成的set/get方法使用的是一个具有相同名称logonButton的ivar。

    -(void)setLogonButton:(UIButton)btn {
    logonButton = [btn retain]; // or whatever the current implementation is
    }

    -(void)setLogonButton:(UIButton)btn {logonButton = [btn retain];//或任何当前实现是}