简体   繁体   中英

objective-c retain , assign and copy?

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.

alloc给出保留计数1。self.objA =将给出保留计数2(由于keep属性)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM