Objective-C Property - The Difference Between Preservation and Assignment

I think I have something missing in the attributes of the properties. Firstly, I can’t understand the difference between retainand assign.

If I use assign, does the property increment the counter retainby 1 on the setter, as well as on the getter, and I need to use releasefor both of them?

And how does it work with readwriteor copy? In terms of retaincount.

I am trying to understand when I need to use releaseafter working with a property (setter and getter)

@property (readwrite,assign) int iVar; 

What is doing assignhere?

What's the difference between:

@property (readwrite,assign) int iVar;

and

@property (readwrite,retain) int iVar;

and

@property (readwrite) int iVar;

Many thanks...

+3
source share
2

: @property (readwrite, assign) int iVar; to @property (readwrite, ) int iVar; to @property (readwrite) int iVar;

@property (readwrite,assign) sometype aProperty;

-(void) setAProperty: (sometype) newValue
{
    ivar = newValue;
}

, ,

@asynthesize aProperty = ivar;

.

@property (readwrite,retain) sometype aProperty;

-(void) setAProperty: (sometype) newValue
{
    [newValue retain];
    [ivar release];
    ivar = newValue;
}

, int, sometype id, SomeObjectiveCClass*

@property (readwrite,copy) sometype aProperty;

-(void) setAProperty: (sometype) newValue
{
    sometype aCopy = [newValue copy];
    [ivar release];
    ivar = aCopy;
}

C, -copyWithZone: (, , NSCopying).

, .

, , , , nonatomic.

+11

:

readwrite , /, , @ synthesize, , .

readonly, , .

, :

assign, , ivar , . , retain .

retain, , , retain, . , , , release - (, dealloc).

copy, , retain copy. , , .

+4

Source: https://habr.com/ru/post/1748840/


All Articles