What equivalent code is synthesized for a declared property?

What do getter and setter body methods look like after they are automatically synthesized?

From the official documentation, I have found so far only the recommended implementation methods, but not a word about which of them are used by the compiler during the synthesis process: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles /mmAccessorMethods.html#//apple_ref/doc/uid/TP40003539-SW5

Some methods recommend implementations that contain a message autoreleasethat is not entirely safe for multithreaded programming. I'm just wondering if the automatically generated code should follow some of the proposed implementations.


For instance:

.h

@interface AClass: NSObject{}
    @property (nonatomic, retain) AnotherClass *aProp;
@end

wow

@implementation AClass
    @synthesize aProp

-(id) init {
    if ((self = [super init])) {
        self.aProp = [[AnotherClass alloc] init];    // setter invocation
    }
    return self;
}

-(AnotherClass *) aMethod {
    return self.aProp;     // getter invocation
}
@end

aProp, ?

-(AnotherClass *) aProp {
    // getter body
}

-(void) setAProp: (AnotherClass *) {
    // setter body
}
+3
2

nonatomic :

// .h
@property (nonatomic, retain) id ivar;

// .m
- (id)ivar {
    return ivar;
}

- (void)setIvar:(id)newValue {
    if (ivar != newValue) {  // this check is mandatory
        [ivar release];
        ivar = [newValue retain];
    }
}

ivar != newValue. , ivar release, retain .

copy, , retain copy.

assign :

- (void)setIvar:(id)newValue {
    ivar = newValue;
}

, atomic ( ), . , , Apple :

- (id)ivar {
    @synchronized (self) {
        return [[self->ivar retain] autorelease];
    }
}

- (void)setIvar:(id)newValue {
    @synchronized (self) {
        if (newValue != self->ivar) {
            [self->ivar release];
            self->ivar = newValue;
            [self->ivar retain];
        }
    }
}

@synchronized retain-autorelease . , ( ), , - , .

+8

, , RETAIN...

- (void)setAProp:(AnotherClass *)value {
   [aProp release];
   aProp = value;
   [aProp retain];
}

ASSIGN....

- (void)setAProp:(AnotherClass *)value {
   aProp = value;
}

COPY ( NSString)...

- (void)setAProp:(AnotherClass *)value {
   aProp = [value copy];
}
0

All Articles