Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.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
Iphone 房地产如何运作?_Iphone_Objective C_Properties - Fatal编程技术网

Iphone 房地产如何运作?

Iphone 房地产如何运作?,iphone,objective-c,properties,Iphone,Objective C,Properties,我正在学习财产,我阅读了各种文档,但仍然不清楚财产是如何工作的,并且遇到了让我困惑的场景。我创建了一个示例应用程序,其中我创建了一个财产,如下所示: @property(nonatomic,retain)NSString *strValue; 并将其合成: @synthesize strValue; 1) 第一种情况: 在viewDidLoad中,我写道: strValue = [[NSString stringWithFormat:@"value"] retain]; self.strV

我正在学习财产,我阅读了各种文档,但仍然不清楚财产是如何工作的,并且遇到了让我困惑的场景。我创建了一个示例应用程序,其中我创建了一个财产,如下所示:

@property(nonatomic,retain)NSString *strValue;
并将其合成:

@synthesize strValue;
1) 第一种情况:

在viewDidLoad中,我写道:

strValue = [[NSString stringWithFormat:@"value"] retain];
self.strValue = [[NSString stringWithFormat:@"value"] retain];
self.strValue = [NSString stringWithFormat:@"value"];
在Dealoc中,我写道:

NSLog(@"str value : %@",self.strValue);
[self.strValue release];
NSLog(@"str value : %@",self.strValue);
[self.strValue release];
NSLog(@"str value : %@",self.strValue);
[self.strValue release];
我的问题是:当我创建保留属性strValue(@property(nonatomic,retain)NSString*strValue;)时,保留的内存发生了什么变化

2) 第二种情况:

在viewDidLoad中,我写道:

strValue = [[NSString stringWithFormat:@"value"] retain];
self.strValue = [[NSString stringWithFormat:@"value"] retain];
self.strValue = [NSString stringWithFormat:@"value"];
在Dealoc中,我写道:

NSLog(@"str value : %@",self.strValue);
[self.strValue release];
NSLog(@"str value : %@",self.strValue);
[self.strValue release];
NSLog(@"str value : %@",self.strValue);
[self.strValue release];
它在self.strValue=[[NSString stringWithFormat:@“value”]retain]行显示内存泄漏。这里的问题是:为什么它在这里显示内存泄漏?这一行是否与以下代码行不等效:

[strValue release];
[strValue retain];
3) 第三种情况: 在viewDidLoad中,我写道:

strValue = [[NSString stringWithFormat:@"value"] retain];
self.strValue = [[NSString stringWithFormat:@"value"] retain];
self.strValue = [NSString stringWithFormat:@"value"];
在Dealoc中,我写道:

NSLog(@"str value : %@",self.strValue);
[self.strValue release];
NSLog(@"str value : %@",self.strValue);
[self.strValue release];
NSLog(@"str value : %@",self.strValue);
[self.strValue release];
它工作得很好,没有任何内存泄漏或悬空引用,如何? 有人能解释一下属性是如何工作的吗?当我们使用属性时,内存是如何分配和释放的

第一个问题,

由于您将实例设置为变量本身,而不是属性,因此您必须分配(或保留)给定实例,如果您为该变量提供了一个自动释放的对象,那么稍后该属性将变为僵尸

第二个问题

否不类似,因为属性已保留实例,另一个retain将增加额外的retain计数,因此您将有一个永远不会释放的额外retain计数

第三项质询

正如我前面所说,该属性将保留该实例,因此将自动发布的实例传递给它不会有问题

下面是一个示例retain属性设置器

- (void) setProperty:(BookItem *)prop
{
    if(_property != prop)
    {       
        [_property release];//release old
        _property = prop;
        [prop retain]; //retain new
    }
}

谢谢你的快速回答。我的很多疑问现在都清楚了。但在我的第一个问题中,我没有使用属性,我直接使用实例变量。所以在这种情况下,它不会释放以前的实例,对吗?是的,不会。你是对的,我没有注意到,如果你想将变量设置为它自己,那么你必须分配,这就是你没有收到泄漏的原因。你能详细说明我得到了什么吗?@Omar Abdelhafith回答得很好,解释得很清楚谢谢@OmarAbdelhafith给出的详细回答。