When do I need to go to the property with me, and when not?

Can someone explain the difference between the installation someObject = someOtherObject;and self.someObject = someOtherObject;if someObject is a class property created using @property (non-atomic, saving) SomeType someObject;

To clarify, I have something like:

@interface SomeClass : NSObject {
   SomeType* someObject;
}

@property (nonatomic, retain) SomeType* someObject;

@end

I noticed that sometimes I get EXC_BAD ACCESS when I use the property without myself, and this seems pretty random. When I use it myself, my program acts as it should. I don't get any compiler errors or warnings when I skip self, so I assume this is somehow valid syntax?

+5
source share
4 answers

- . , @property (, ) SomeType * someObject; , 2 :

getter:

-(SomeType*) someObject {
   return someObject;
}

-(void) setSomeObject:(SomeType*) obj {
   [someObject release];
   someObject = [obj retain];
}

, ivars , setter/getter ( ). someObject = new_val, . , .

: . / , . , A , B , - , A.

. , , .

: , , /, (, , ...), . , , , .

+2

self.someObject = someOtherObject . . retain , , , , retain, 1. , - release, .

Obects , 0. EXC_BAD_ACCESS ecxeption, (, ).

:

SomeOtherObject *soo = [[SomeOtherObject alloc] init]; //retain count: 1
self.someObject = soo; //soo retain count is now 2
[soo release]; //soo retain count is 1 again, as self still uses it.
[self doSomethingWithSoo];

, , soo.

SomeOtherObject *soo = [[SomeOtherObject alloc] init]; //retain count: 1
someObject = soo; //soo retain count is still  1
[soo release]; //soo retain count is 0, it will be deallocated
[self doSomethingWithSoo]; //will fail with an EXC_BAD_ACCESS exception, as soo does not exist anymore.
+4

.

class someObject @property/@synthsize .h/.m.

someObject, . self.someObject, accessor [self someObject] whitch .

, , self.someObject = someOtherObject;, . @property (nonatomic, retain), .

+1

:

1), "". -.

2), "". setter . , [self setMyObject:...];

self.myobject , ( self), alloc, .

, "self.", .

, self.someObject = [someOtherObject retain]

0

All Articles