What is the meaning of nonatomic and retain in a property declaration

10,053

Solution 1

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.

Solution 2

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.

Solution 3

You should probably read the Declared Properties chapter of The Objective C Programming Language.

Solution 4

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];
}

Solution 5

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

Share:
10,053
A for Alpha
Author by

A for Alpha

iOS application developer and a huge steve Jobs fan. The beauty of the apple products motivated me to be a iOS apps developer and i am in love with it... :)

Updated on August 24, 2022

Comments

  • A for Alpha
    A for Alpha over 1 year

    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 of the (nonatomic, retain or assign or copy)function.. Can anyone help me with this.. Thankyou