What is the meaning of nonatomic and retain in a property declaration
i'm new to iOS programming. Can anyone tell me the exact meaning of the following line of code
@property(**nonatomic, retain**) UIView *singleTapView;
i have been using @property for many a times with out actually knowing the exact meaning 开发者_Go百科of the (nonatomic, retain or assign or copy)
function.. Can anyone help me with this..
Thankyou
This is a question that should be brought up more frequently.
@property
is a simple property declaration. Nothing new here.
nonatomic
means that there is no object locking implemented for the corresponding @synthesize
accessor, the property is just provided directly. This is faster than atomic
, but can result in partially written values etc. in multithreaded use cases.
If you use the default (which is atomic
), then the @synthesized methods use an object-level lock to ensure that multiple reads/writes to a property are serialized. As the Apple docs point out, this doesn't mean the whole object is thread-safe, but the property reads/writes are.
If you write your own accessor methods, this does nothing though. But most programmers write it anyway.
The retain thing is a little simpler. Basically, it means that you want an object that uses the
alloc
-> init
-> retain
-> release
cycle of doing things. Basically, you're going to be using it for everything but primitives like Booleans and Integers.
Properties are used in iOS to replace the getter and setter methods which we normally write.
Your line of code:
@property(nonatomic, retain) UIView *singleTapView;
means that you are writing the getter and setter methods for your UIView.
And it'll automatically retain or increment the retain count of your UIView whenever you use it anywhere in your code.
However, when you use:
@property(nonatomic, assign) UIView *singleTapView;
and then use your UIView, its retain count won't increase. This means that it will not retain your UIView.
And "copy" is just used to give the value of your current object to a new object.
You should probably read the Declared Properties chapter of The Objective C Programming Language.
when you declare @property you are implicitly creating getter and setter method of that particular variable....
and how that getter and setter works is depends upon the how you declare @property
for example
@property(nonatomic,retain)
will make setter method in which its retain count will be increased and the the variable will not be thread safe ...
Every time you type self.variableName
it called its setter method which is created by @property
For the nonatomic/atomic part, you should read Atomic Operation. It is not specific to iOS but will give you better understanding
For the retain part, this code will help you. It is similar to what the @synthesize
will generate for you
//getter
- (Book *)book
{
return [[book retain] autorelease];
}
//setter
- (void)setBook:(Book *)aBook
{
if (book == aBook)
{
return;
}
Book *oldBook = book;
book = [aBook retain];
[oldBook release];
}
精彩评论