弱势属性& prepareForSegue:一个属性变为null而另一个属性不变

时间:2015-07-07 15:44:23

标签: ios objective-c properties weak-references strong-references

我真的很困惑prepareForSegue在弱/强属性中发生的事情。让我解释一下:

我有两个简单的视图控制器

  • ViewController1
  • ViewController2

从ViewController1与ViewController2的 one segue 连接。

两个视图控制器都有两个属性:

@property (nonatomic, weak) NSArray *simpleArray;
@property (nonatomic, weak) NSString *simpleString;

我无法理解的是,在我的prepareForSegue中, NSArray变为null但NSString不是。这是来自ViewController1的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.simpleString = @"string";
    self.simpleArray = [NSArray arrayWithObjects:@"string2", @"string3", nil];

    NSLog(@"ViewDidLoad - simpleString: %@, simpleArray: %@", self.simpleString, self.simpleArray);
}


#pragma mark - segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"PrepareForSegue - simpleString: %@, simpleArray: %@", self.simpleString, self.simpleArray);

    if ([[segue identifier] isEqualToString:@"segue"])
    {
        ViewController2 *vc = (ViewController2 *)segue.destinationViewController;
        vc.simpleString = self.simpleString;
        vc.simpleArray = self.simpleArray;
    }
}
  

2015-07-07 17:23:12.915 test [3675:147600] ViewDidLoad - simpleString:   string,simpleArray :(       字符串2,       string3)

     

2015-07-07 17:23:14.521 test [3675:147600] PrepareForSegue - simpleString:string,simpleArray:(null)

如果我将NSArray属性设置为strong,则保持该值。

那么,为什么两个弱属性有不同的行为?

0 个答案:

没有答案