Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 分配给保留属性的已分配对象的保留计数_Objective C_Uiimage_Retaincount - Fatal编程技术网

Objective c 分配给保留属性的已分配对象的保留计数

Objective c 分配给保留属性的已分配对象的保留计数,objective-c,uiimage,retaincount,Objective C,Uiimage,Retaincount,在下面的代码中,我希望retain计数增加到2,但赋值后它仍然保持为1。分配给具有retain限定符的属性。保留将使对象的保留计数增加1。有人能解释为什么保留计数没有增加吗 MyClass.h: @property (nonatomic,retain) UIImage * imageBackground; MyClass.m: UIImage * IMAGE = [[UIImage alloc] initWithContentsOfFile:@"image.png"]; NSLog(@"ret

在下面的代码中,我希望retain计数增加到2,但赋值后它仍然保持为1。分配给具有retain限定符的属性。保留将使对象的保留计数增加1。有人能解释为什么保留计数没有增加吗

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.
您就不会使用属性的setter,因此保留计数不会更改,因为这只是对ivar的简单指针赋值。

谢谢!这是一个解决了我代码中许多问题的答案,我已经为它挠头好几个小时了!
self.imageBackground = IMAGE;