为什么 `assign` 属性 没有在 ARC 下释放?纯粹的运气?
Why isn't the `assign` property deallocated under ARC? Pure luck?
更新: 我认为 ARC 下的默认行为是 assign
,但它是 strong
。所以,不要费心阅读问题,它没有用:)
考虑以下代码:
#import "AppDelegate.h"
@interface TestClass: NSObject
@property (atomic) NSMutableArray *strings; //"assign" by default
@end
@implementation TestClass
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
TestClass *testObject = [TestClass new];
testObject.strings = [[NSMutableArray alloc] init];
//(*) Why isn't strings array deallocated here under ARC? There are no strong references to it.
[testObject.strings addObject:@"str1"];
[testObject.strings addObject:@"str2"];
[testObject.strings addObject:@"str3"];
NSLog(@"Strings: %@", testObject.strings);
return YES;
}
@end
这里strings
属性声明为assign
(默认)。所以,如果我没记错的话,代码中根本没有对这个数组的强引用。所以,从我的角度来看,strings
应该在 (*)
处被释放。但是,代码可以工作并打印数组。
为什么?我可能的解释是NSMutableArray
有一些实现细节,所以strings
数组还有一些内部引用,所以它仍然有效。所以这只是纯粹的运气。我对吗?我欺骗系统返回 retainCount
,它等于 2
在 NSLog
.
点
如果我将 属性 声明更改为 (atomic, weak)
,数组将如预期的那样 nil
。
我使用 Xcode 7.1.1 (7B1005) 和 OS X 10.11.2 (15C30)。我在 iOS 模拟器中检查了 DEBUG 版本。
我在 Internet 上找到了这样的代码,并预计它会崩溃,但它没有。因此问题。
在 ARC 下,默认所有权是 strong
而不是 assign
,有关详细信息,请参阅 Clang ARC documentation。
更新: 我认为 ARC 下的默认行为是 assign
,但它是 strong
。所以,不要费心阅读问题,它没有用:)
考虑以下代码:
#import "AppDelegate.h"
@interface TestClass: NSObject
@property (atomic) NSMutableArray *strings; //"assign" by default
@end
@implementation TestClass
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
TestClass *testObject = [TestClass new];
testObject.strings = [[NSMutableArray alloc] init];
//(*) Why isn't strings array deallocated here under ARC? There are no strong references to it.
[testObject.strings addObject:@"str1"];
[testObject.strings addObject:@"str2"];
[testObject.strings addObject:@"str3"];
NSLog(@"Strings: %@", testObject.strings);
return YES;
}
@end
这里strings
属性声明为assign
(默认)。所以,如果我没记错的话,代码中根本没有对这个数组的强引用。所以,从我的角度来看,strings
应该在 (*)
处被释放。但是,代码可以工作并打印数组。
为什么?我可能的解释是NSMutableArray
有一些实现细节,所以strings
数组还有一些内部引用,所以它仍然有效。所以这只是纯粹的运气。我对吗?我欺骗系统返回 retainCount
,它等于 2
在 NSLog
.
如果我将 属性 声明更改为 (atomic, weak)
,数组将如预期的那样 nil
。
我使用 Xcode 7.1.1 (7B1005) 和 OS X 10.11.2 (15C30)。我在 iOS 模拟器中检查了 DEBUG 版本。
我在 Internet 上找到了这样的代码,并预计它会崩溃,但它没有。因此问题。
在 ARC 下,默认所有权是 strong
而不是 assign
,有关详细信息,请参阅 Clang ARC documentation。