-3

Possible Duplicate:
@property @synthesize

I'm a little confused on the 2 methods, could someone please explain them to me?

What do they do, and why are they better than just using -(void)variable; and -(void)variable{}?

Community
  • 1
  • 1
Patrick
  • 19
  • 4

2 Answers2

0

It's just a more convenient way to define standard getter/setter methods for your variables, because writing all over and over again simple standard getter and setter methods can be a real pain in the ... And properties provide an easy way to memory management (e.g. strong, nonatomic and so forth).

Cravid
  • 653
  • 2
  • 7
  • 22
0

What do they do

They declare and implement property accessor methods (the getter and setter), respectively. @property declares, @synthesize tells the compiler to issue an autogenerated implementation for the declared methods.

why are they better than just using -(void)variable; and -(void)variable{}?

Because they're shorter, so more concise and make code more readable. Also, they have no errors in themselves - if you were to write a bunch of accessor methods, I'm sure you'd eventually miss something and you couldn't for the love of God tell where a mysterious segmentation fault came from. This doesn't happen with declared properties (so they are called).

One minor caveat is that old Objective-C compilers don't support declared properties. It might be the case (although there's very little chance for it) that one day you'll need to compile your code with an old compiler and it would be impossible because of this syntax. But again, this is very unlikely to happen.