简体   繁体   English

ios伙伴分配之间有什么区别?

[英]ios what`s the different between the fellow alloc?

method1: 方法一:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                         [NSURLRequest requestWithURL:
                          [NSURL URLWithString:appRecord.imageURLString]] delegate:self];
self.imageConnection = conn;
[conn release];

method2: 方法2:

self.imageConnection = [[NSURLConnection alloc] initWithRequest:
                        [NSURLRequest requestWithURL:
                         [NSURL URLWithString:appRecord.imageURLString]] delegate:self];

Answer: it depends. 答:这要看情况。 I'm assuming that your self.imageConnection property is set to retain. 我假设您的self.imageConnection属性设置为保留。

If you have Automatic Reference Counting (ARC) enabled, there isn't really a difference (that you need to know about), because the compiler will take care of your memory management for you...of course, if you did have ARC enabled you could only use the second example you give, because you can't call release . 如果启用了自动引用计数(ARC),则实际上并没有什么不同(您需要了解),因为编译器将为您完成内存管理……当然,如果您确实有ARC,启用后,您只能使用您提供的第二个示例,因为您不能调用release

In a non-ARC environment there is quite a big difference: the first method you posted releases its memory correctly, and the second one leaks it. 在非ARC环境中,有很大的不同:您发布的第一种方法可以正确释放其内存,而第二种方法则可以泄漏内存。

To explain, if you declare a property as retain it will, as the name suggests, retain it. 解释一下,如果将一个属性声明为retain ,则顾名思义,它将保留它。 When you create your NSURLConnection you alloc/init it, which retains it once. 创建NSURLConnection时,将对其进行分配/初始化,该操作将保留一次。 You then assign it to self.imageConnection - doing so increments the retain count again. 然后将其分配给self.imageConnection这样做会再次增加保留计数。

In your first example you've used a temporary variable, which means once you've assigned the property you can call release to 'normalise' the retain count. 在第一个示例中,您使用了一个临时变量,这意味着一旦分配了属性,便可以调用release来“标准化”保留计数。 But in your second example you have no temporary variable - you've lost the reference, and by assigning it to a property you've retained it twice. 但是在第二个示例中,您没有临时变量-您丢失了引用,并且通过将其分配给属性将其保留了两次。

On solution to this is to add an autorelease statement when you assign the property, like this: 解决此问题的方法是在分配属性时添加autorelease语句,如下所示:

self.imageConnection = [[NSURLConnection alloc] initWithRequest: [NSURLRequest 
                                                 requestWithURL: [NSURL URLWithString:appRecord.imageURLString]] delegate:self] 
                                                 autorelease];

... or you could just do what you show in your first example, and use a temporary variable. ...或者您可以按照您在第一个示例中显示的内容进行操作,并使用一个临时变量。

This doesn't hold true for all properties - when you declare you property you also declare whether you'd like it to be retained ('retain'), just kept as a pointer ('assign'), or copied ('copy'). 这并非对所有属性都成立-当您声明属性时,还声明是要保留它('retain'),还是将其保留为指针('assign')还是复制('copy' )。 An assigned property wouldn't have this problem. 分配的属性不会有此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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