use of @property and @synthesise?

懵懂的女人 提交于 2019-12-01 08:40:38

@property and @synthesize are two objective C keyword that allow you to easily create your properties and therefore avoid to write by hand getters and setters methods of the property.

The @property define the property itself, should be placed in the header file and can get some attributes (as for example : strong, nonatomic, retain assign, copy), the @synthesize should be placed into the implementation file and tell the compiler to generate the body of getter and setter method.

These two keyword are extremely useful when coupled with the right use of their attributes, because they take care of the generation of the property code and most of all they take care of the memory management of the property.

@property - create the declaration of your getter and setter.

@synthesize - provide the definition of getter and setter based upon the parameters which are passed inside property.

Check this out, there are a lot more details about the same present there - https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html

anonymous

on using @property the compiler will take care of declaring getter and setter methods based on readonly and readwrite

readonly -> getterMethod

readwrite -> both setter and getter method on using @synthesize the compiler will take care of defining getter and setter methods

If you have an instance variable (ivar) in your class, you can't access it from other classes usually. So you have to make public accessor methods (getters and setters). They look something like this:

Setter:

- (void)setMyVariable:(SomeClass *)newValue {
    if (newValue != myVariable) {
        [myVariable release];
        myVariable = [newValue retain];
    }
}

Getter:

- (SomeClass *)myVariable {
    return myVariable;
}

This was the way you had to do it before Objective-C 2.0. Now you can use @property and @synthesize to speed this up. It's basically just a shortcut.

In the header you use @property to define what kind of setters you want. Should the setter retain the passed value (like in my example) or copy or just assign?

And in the implementation you just write @synthesize to make the compiler include the automatically created getters and setters at that position. Usually at the top of your implementation.

My feeling is that all iVars should have an associated underscore synthesised property (using an _iVar prevents accidental direct access), and all access to the iVars, apart from init and dealloc methods, should via the property.

IMHO the big win is memory management - it's safer and much easier as there is no need to remember which iVars have been retained.

And think of how much work is required to code an accessor - 4 lines for getter and 2 for a setter.

At some point in the future @synthesize is likely to be optional, so all you'll need is the @property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!