-2

Here is my code. "ViewController.m"

-(void)viewDidLoad {
    [super viewDidLoad];
    Fruit *item1 = [Fruit new];
    item1.name = @"Apple";
    item1.price = @"1$ for one";
    item1.image = @"apple.jpeg";
    item1.location = [NSArray arrayWithObjects:@"Japan", @"Apple is good for health.", nil];
    Fruit *item2 = [Fruit new];
    item2.name = @"Banana";
    item2.price = @"80 cents for one";
    item2.image = @"banana.jpeg";
    item2.location = [NSArray arrayWithObjects:@"Taiwan",@"Banana is good after exercise.", nil]
    fruit = [NSMutableArray arrayWithObjects:item1,item2, nil];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Fruit *newRow = [fruit objectAtIndex:indexPath.row];
    NSString * selectedFruit = newRow.name;
    UIAlertView * messageAlert = [[UIAlertView alloc]initWithTitle:@"Row Selected"message:selectedFruit delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"hi", nil];
    [messageAlert show];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
    if ([segue.identifier isEqualToString:@"showFruitDetail"])
    {
        NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
        FruitDetailViewController * destViewController = segue.destinationViewController;
        Fruit *newRow = [fruit objectAtIndex:indexPath.row];                 
        destViewController.detailFruit = newRow;
    }
}

I put breakpoints in both of the two functions and check the parameter "location". They both show "nil".

The parameter "location" is also an array. How to deliver arrays among functions?

gabriel
  • 2,351
  • 4
  • 20
  • 23
  • 1
    can you copy paste your Fruit class properties – Anupam Apr 02 '15 at 06:57
  • 1
    Welcome to the community, you probably should take a look at:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/FirstTutorial.html – LoVo Apr 02 '15 at 06:57
  • What kind of reference your property location used weak or strong?. In my though It should be strong to avoid deallocation of the collection – Nitheesh George Apr 02 '15 at 07:11
  • Thanks guys. I just replaced the property location weak with strong and the problem was solved !! I am a rookie in iOS programming. What's the difference between weak and strong? It means the priority? – Wang Benjamin Apr 02 '15 at 07:15
  • please check my answer below to understand the different between strong vs weak reference – Nitheesh George Apr 02 '15 at 08:09

1 Answers1

0

The "location" property should use "strong" reference to avoid automatic deallocation by ARC.

@property(nonatomic, strong) NSArray * location;

Check the following link in Stackoverflow to understand the Strong vs weak reference Differences between strong and weak in objective-c

Community
  • 1
  • 1
Nitheesh George
  • 1,357
  • 10
  • 13