【发布时间】:2017-05-08 11:24:18
【问题描述】:
我知道 strong 和 weak 是在属性声明中使用的修饰符,而 __strong 和 __weak 在实例变量的声明中使用...strong 表示只要我拥有它就将对象保留在内存中,而 weak 表示保留该对象只要其他人对此有强烈的参考,就在记忆中……对吗?但我不明白为什么弱属性和 __weak 实例变量的行为不同?这就是我想知道的......
@interface DemoViewController (){
__weak NSArray *weakArray;
__strong NSArray *strongArray;
__weak NSString *weakString;
__strong NSString *strongString;
}
@property (weak) NSString *weakStringProperty;
@property (strong) NSString *strongStringProperty;
@property (weak) NSArray *weakArrayProperty;
@property (strong) NSArray *strongArrayProperty;
@end
@implementation DemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
strongArray = [[NSArray alloc] initWithObjects:@"one",@"two", nil];
weakArray = strongArray;
NSLog(@"Round:1 strongArray is %@.", strongArray);
NSLog(@"Round:1 weakArray is %@.", weakArray);
strongArray = nil;
NSLog(@"Round:2 strongArray is %@.", strongArray);
NSLog(@"Round:2 weakArray is %@.", weakArray);
self.strongArrayProperty = [[NSArray alloc] initWithObjects:@"one",@"two", nil];
self.weakArrayProperty = self.strongArrayProperty;
NSLog(@"Round:1 strongArrayProperty is %@.", self.strongArrayProperty);
NSLog(@"Round:1 weakArrayProperty is %@.", self.weakArrayProperty);
self.strongArrayProperty = nil;
NSLog(@"Round:2 strongArrayProperty is %@.", self.strongArrayProperty);
NSLog(@"Round:2 weakArrayProperty is %@.", self.weakArrayProperty);
strongString = [[NSString alloc]initWithFormat:@"instanceVariable"];
weakString = strongString;
NSLog(@"Round:1 strongString is %@.", strongString);
NSLog(@"Round:1 weakString is %@.", weakString);
strongString = nil;
NSLog(@"Round:2 strongString is %@.", strongString);
NSLog(@"Round:2 weakString is %@.", weakString);
self.strongStringProperty = [[NSString alloc]initWithFormat:@"Property"];
self.weakStringProperty = self.strongStringProperty;
NSLog(@"Round:1 strongStringProperty is %@.", self.strongStringProperty);
NSLog(@"Round:1 weakStringProperty is %@.", self.weakStringProperty);
self.strongStringProperty = nil;
NSLog(@"Round:2 strongStringProperty is %@.", self.strongStringProperty);
NSLog(@"Round:2 weakStringProperty is %@.", self.weakStringProperty);
}
@end
这是结果日志
Round:1 strongArray is (
one,
two
).
Round:1 weakArray is (
one,
two
).
Round:2 strongArray is (null).
Round:2 weakArray is (null).
Round:1 strongArrayProperty is (
one,
two
).
Round:1 weakArrayProperty is (
one,
two
).
Round:2 strongArrayProperty is (null).
Round:2 weakArrayProperty is (
one,
two
). —???
Round:1 strongString is instanceVariable.
Round:1 weakString is instanceVariable.
Round:2 strongString is (null).
Round:2 weakString is (null).
Round:1 strongStringProperty is Property.
Round:1 weakStringProperty is Property.
Round:2 strongStringProperty is (null).
Round:2 weakStringProperty is Property. ——??
在它们弱引用的对象之后,两个弱实例变量 print (null) 都设置为 nil,这正如预期的那样,但我想知道为什么弱属性 weakStringProperty 和 weakArrayProperty 仍然打印它们以前的值并且表现得像以前一样分别强烈指向strongStringProperty和strongArrayProperty??
谢谢:)
【问题讨论】:
-
我能够在第 2 轮中将 strongArrayProperty 和 weakArrayProperty 作为(null)。虽然,第 2 轮中的 weakStringProperty 仍然是“属性”。这很奇怪,因为 ARC 应该在技术上处理这个问题。 PS-我在我的电脑上运行了这个。
-
我认为原因是因为 main 方法有一个封闭应用程序运行的自动释放池,所以保留了弱属性。由于自动释放池,weak 属性不会立即设置为 nil。虽然,我尝试在 viewDidAppear 和 didReceiveMemoryWarning 中访问它(通过模拟内存警告),它仍然没有变成 nil。
-
奇怪!!当我在实际设备上运行此代码时,它给出了弱实例变量和弱属性的预期结果......这意味着问题仅出在模拟器上。无论如何谢谢@Kunal Shrivastava
标签: ios objective-c memory-management properties