0

I deleted the previous post ...

Which one do i use if I have a class that owns an object objA? I know if the object wasn't own by the class,it needs to be retained.

@class A

@property (retain) ObjectA objA;

@end

@implementation A

-(void) func {
   self.objA = [[ObjectA alloc] init];
}

@end

If objA in class A is set as a retain, and was initialized in func using init function. Would this give 2 retain count or just 1 retain count.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • possible duplicate of [What does @property(retain) do?](http://stackoverflow.com/questions/6360499/what-does-propertyretain-do) – jscs May 25 '12 at 07:32

2 Answers2

1

alloc gives retain count 1. self.objA = will give retain count 2 (because of the retain property)

whooops
  • 935
  • 6
  • 11
0

I would have done :

   - (void) func {objA = [[ObjectA alloc] init]; }

or

   - (void) func { self.objA = [[[ObjectA alloc] init] autorelease]; }

to prevent memory leaks. However I prefer the first solution