简体   繁体   English

iOS关于弱点和强点,结果应该是什么? 并不断声明

[英]IOS about weak and strong, what's the result should be? and constant declare

there are two properties like below 有如下两个属性

#import <Foundation/Foundation.h>

@interface Contact : NSObject

@property(nonatomic, strong)NSDate *birthDay;
@property(nonatomic, weak)NSDate *birthDay1;

- (void)testWeakProperty;

@end

on implementation file as: 在实现文件上为:

- (void)testWeakProperty {
    self.birthDay = [NSDate dateWithTimeIntervalSinceNow:0];
    self.birthDay1 = self.birthDay;
    self.birthDay = nil;
    NSLog(@"_birthday1 is %@, %@", self.birthDay1 , self.birthDay);
}

why the result is not _birthday1 is (null), (null) ? 为什么结果不是_birthday1(null), (null)

I found that iOS ARC - weak and strong properties . 我发现iOS ARC具有弱和强属性 if self.birthDay is constant, it will not be deallocated. 如果self.birthDay为常数,则不会释放它。 but there is [NSDate dateWithTimeIntervalSinceNow:0] , 但是有[NSDate dateWithTimeIntervalSinceNow:0]

now I want to know if the return value is constant and how to verify declare result is constant and a variable. 现在我想知道返回值是否为常数,以及如何验证声明结果是否为常数和变量。

The key here is that you're dealing with an autorelease object. 关键是要处理自动释放对象。 If a method name starts with init , copy , mutableCopy or new , you'll receive a non-autorelease object. 如果方法名称以initcopymutableCopynew开头,则您将收到一个非自动释放的对象。 This is not the case here (you are using dateWithTimeIntervalSinceNow ) and as a result, you'll receive an autorelease object. 此处不是这种情况(您正在使用dateWithTimeIntervalSinceNow ),因此,您将收到一个自动释放对象。

Thus, you are instantiating an autorelease object and therefore it will not be deallocated until the autorelease pool is drained. 因此,您正在实例化一个自动释放对象,因此,直到耗尽了自动释放池之后,该对象才会被释放。 And your weak reference will not be nil -ed until the object is deallocated. 和你的弱引用将不会是nil ,直到该对象被释放-ed。 The deallocation of autorelease objects happens when your app yields back to the run loop (or you explicitly create your own autorelease pool). 当您的应用退回到运行循环时(或您显式创建自己的自动释放池),自动释放对象将被释放。

This not a question of the object being "a constant". 这不是对象是“常数”的问题。 And that other question you reference is discussing NSString which, being heavily optimized, does not conform to traditional object memory management rules. 您引用的另一个问题是讨论NSStringNSString经过了优化,不符合传统的对象内存管理规则。

You can change the behavior by explicitly adding your own autorelease pool, which will cause the object to be deallocated when the pool is drained at the end of the @autoreleasepool block, and you'll see your (null), (null) response: 您可以通过显式添加自己的自动释放池来更改行为,当在@autoreleasepool块末尾耗尽该池时,这将导致对象被释放,您将看到(null), (null)响应:

@autoreleasepool {
    self.birthDay = [NSDate dateWithTimeIntervalSinceNow:0];
    self.birthDay1 = self.birthDay;
    self.birthDay = nil;
}
NSLog(@"_birthday1 is %@, %@", self.birthDay1 , self.birthDay);

You can also use a non-autorelease object (eg use a method whose name starts with init ) and this non-autorelease object will be deallocated immediately after being set to nil : 您还可以使用非自动释放对象(例如,使用名称以init开头的方法),并且将此非自动释放对象设置为nil后将立即释放该对象:

self.birthDay = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
self.birthDay1 = self.birthDay;
self.birthDay = nil;
NSLog(@"_birthday1 is %@, %@", self.birthDay1 , self.birthDay);

This will also show you (null), (null) . 这也会显示(null), (null)

As Rob explaining, the important point is the fact you are using an auto-released object. 正如Rob解释的那样,重要的一点是您正在使用自动释放的对象。

Just a small precision about his explanation, [NSDate date...] is in fact a short ( convenience ) version of : [NSDate date...]只是关于他的解释的一个小精度,实际上是以下内容的简短( 方便 )版本:

[[[NSDate alloc] initWith...] autorelease]

Which behave differently than an non-autoreleased instance: 与非自动发布实例的行为不同:

[[NSDate alloc] initWith...]

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

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