2

I'm really confused about what happens in prepareForSegue at weak/strong properties. Let me explain:

I have two simple view controller:

  • ViewController1
  • ViewController2

connected with one segue from ViewController1 to ViewController2.

Both view controllers have two properties:

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

The thing that I can't understand is that in my prepareForSegue the NSArray becomes null but the NSString not. This is the code from 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: ( string2, string3 )

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

If I set the NSArray property as strong it maintain the value.

So, why two weak properties have a different behavior?

Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54

0 Answers0