开发者

Retain count of allocated object assigned to a retained property

开发者 https://www.devze.com 2023-02-17 06:55 出处:网络
In the following code I am expecting the retain count to increase to 2, but it remains at 1 after assignment.The assignment is to a property with 开发者_如何学Pythona retain qualifier.A retain will in

In the following code I am expecting the retain count to increase to 2, but it remains at 1 after assignment. The assignment is to a property with 开发者_如何学Pythona retain qualifier. A retain will increment the retain count of the object by 1. Can anyone explain why the retain count is not incremented?

MyClass.h:

@property (nonatomic,retain) UIImage * imageBackground;

MyClass.m:

UIImage * IMAGE = [[UIImage alloc] initWithContentsOfFile:@"image.png"];
NSLog(@"retain-count(%d)", [IMAGE retainCount]); // returns 1
imageBackground = IMAGE;
NSLog(@"retain-count(%d)", [IMAGE retainCount]); // returns 1, should return 2


self.imageBackground = IMAGE;

Without the self. you are not using the property's setter, and thus the retain count won't change since that was just a simple pointer assignment to an ivar.

0

精彩评论

暂无评论...
验证码 换一张
取 消