When do you have to use @property and @synthesize in the iPhone SDK?

后端 未结 2 647
傲寒
傲寒 2020-12-18 17:12

When do you have to use @property and @synthesize in the iPhone SDK? And why use @property and @synthesize? I was studyin

2条回答
  •  有刺的猬
    2020-12-18 17:55

    @property : you used it when you want to:

    You can use some of the really useful generated code like nonatomic, atmoic, retain without writing any lines of code. You also have getter and setter methods. To use this, you have 2 other ways: @synthesize or @dynamic: @synthesize, compiler will generate the getter and setter automatically for you, @dynamic: you have to write them yourself.

    @property is really good for memory management, for example: retain.

    How can you do retain without @property?

      if (_variable != object) {
        [_variable release];
        _variable = nil;
        _variable = [object retain];
      }
    

    How can you use it with @property?

    self.variable = object;
    

    When you are calling the above line, you actually call the setter like [self setVariable:object] and then the generated setter will do its job

提交回复
热议问题