0

Possible Duplicate:
Why should I use @properties?
When to use properties in objective C?

I have been programming in objective-c for a little over a year now, and I always felt like it was a convention to use @property and @synthesize. But what purpose do they really serve ? Are they solely there to be communicated between classes ? So, for instance, if I use an NSMutableDictionary only in the scope of the class where it was declared, an omission is O.K. ?

Another question:

If I set the property of an NSMutableDictionary, it is retained, right ? So, in my class I don't have to call alloc() and init(), do I ? What are the rules to use properties ?

Community
  • 1
  • 1
the_critic
  • 12,720
  • 19
  • 67
  • 115
  • 2
    When you remember that a `@property` declaration is simply declaring `-setFoo:` and `-foo` methods, this question is essentially the same question that has been asked forever with other languages like C++ and Java: "Shoud you use accessor methods or directly access the instance variable?". In the case of Objective-C there is also the issue of safe memory management. – Mike Weller May 22 '12 at 12:09
  • I already know, that getters and setters are set up. But what else happens under the hood ? If I only declare an NSMutableDictionary w/o @property, will I still be able to use it in my class as before ? I don't want other classes to know about them. – the_critic May 22 '12 at 12:14
  • @MikeWeller not quite the same; it is very important to remember that you pretty much **never ever** access ivars directly in objective-c. – bbum May 22 '12 at 15:25
  • 2
    http://stackoverflow.com/questions/8194281/, http://stackoverflow.com/questions/3649227/, http://stackoverflow.com/questions/785059/, http://stackoverflow.com/questions/4299487/ http://stackoverflow.com/questions/1082284/, http://stackoverflow.com/questions/2989830/, http://stackoverflow.com/questions/4333797/, http://stackoverflow.com/questions/10137016/, http://stackoverflow.com/questions/4945091/ – jscs May 22 '12 at 16:27
  • You can also read this blog http://objective-c-properties.blogspot.in/ – Inder Kumar Rathore May 22 '12 at 16:49

3 Answers3

6

But what purpose do they really serve?

Access control to iVars and abstraction between representation and underlying data.

Are they solely there to be communicated between classes?

No, they are for when you want to control access to iVars instead of accessing them directly or when you could in the future change underlying data structures but wish to keep the current representation.

So, for instance, if I use an NSMutableDictionary only in the scope of the class where it was declared, an omission is O.K.?

It depends. Do you want to have controlled access to the iVar? Would it be possible for your code to change so the dictionary is fetched and not a direct iVar. Usually, the answer is yes.

If I set the property of an NSMutableDictionary, it is retained, right?

Depends on how you declare the property.

So, in my class I don't have to call alloc() and init(), do I?

You have sloppy wording here. I think you are asking if you still need to construct an instance of a property. Yes, you will need to construct an instance of a property in some way. There are lots of ways of doing this.

NOTE: the convention for talking about methods is use their signature. Instead of alloc(), you would use -alloc.

What are the rules to use properties?

This you will need to read the doc for.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
1

Like in another languages, when we want to make our variable global or public we use public access modifier. In objective c when we want access our another class variable in other class, we use @property and @synthesize them. Basically @synthesize is way by which compiler create a setter and getter methods for that variable. You can manually create them but not use @synthesize. By creating object of that class you can access your property variable in other class. By using retain, you clear that is take place memory and not exist until that container class not goes dispose or released.

Lalit Kumar
  • 121
  • 1
  • 3
0

Properties simply make your life easier.

Nowadays use properties as much as you can in terms of memory management, code-style and timesaving.

What do @propertys do?
They can create getter and setter methods (depends on given parameters).

Normally you declare instance variables in the header file (like in c++).
Now you simply let that be and instead of that declare the properties you want for instance variables.

Properties can get multiple arguments.
For normal objective-c objects, where you need a pointer (*) you would write.
@property (nonatomic,retain,...)

When you @synthesize it it creates a getter and a setter.
The setter automatically does stuff like releasing your old object, that your variable hold and retaining the new one.
So you don't have to do that manually (which should be quite often the case). Thats important.
You also can give it arguments (readonly,readwrite) to decide if to set a setter or not.
You can even declare a @property in the header file readonly and override that in your implementation file with a extension (a category with no name).

To dive deeper into this, read the apple developer manuals, which are quite effective. Hope that helps a bit.

Shure it is the tip of the iceberg, but it's mostly everything you need.

Fab1n
  • 2,103
  • 18
  • 32