tags:

views:

46

answers:

2

when u have to use @property and @synthesize in iphone sdk? and why use @property @synthesize? i was study about property ... but i can't get a correct idea...

so please help me... and give some idea and example.....please

+1  A: 

@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

vodkhang
Quick sidenote - if you're doing the retain without @property, be sure and check for self assignment! if (variable_ != object) [variable_ release]; Otherwise you may end up releasing your only reference to the object, and calling retain on it after it's released will be bad.
Austen Green
ah yeah,that's right
vodkhang
i want some specific situation for using the iphone sdk
heyram
It is for iphone sdk. They are jobs I do all the time in iphone programming
vodkhang
+1  A: 

@property (along with @synthesize) automatically generates set and/or get code. So the following code:

self.prop = @"some string";

is equivalent to

[self setProp: @"some string"];

Note also,

self.prop = @"some string";

is different from

prop = @"some string";

The latter sets the variable directly, whereas the former calls the method getProp to set the variable prop.

William