A for Alpha
A for Alpha

Reputation: 2912

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

Upvotes: 3

Views: 10106

Answers (5)

onmyway133
onmyway133

Reputation: 48115

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

Upvotes: 1

mayuur
mayuur

Reputation: 4746

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.

Upvotes: 4

Alex Deem
Alex Deem

Reputation: 4805

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

Upvotes: 4

Aurum Aquila
Aurum Aquila

Reputation: 9126

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.

Upvotes: 9

Mihir Mehta
Mihir Mehta

Reputation: 13833

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

Upvotes: 1

Related Questions