简体   繁体   中英

Count of NSMutableArray is 0 after adding object

hi i have an object (objectA) with tow instance variables, NSString and NSDate. and i have two other objects one with tableView and add button (objectB) and one presented modally when you press the add button (objectC) and in this object view you can type name and date and when this object (objectC) dismissed i create new object (objectA) with name and date.

objectB have NSMutableArray and i want to add objectA to this array so it can be appear in tableView and i do it like this in objectC.m

- (IBAction)saveButtonPressed {
   objectA *a = [[objectA alloc] init];
   [a setName:[myUITextField text]];
   [a setDate:[myDatePicker date]];

   objectB *b = [[objectB alloc] init];
   [[b myMutableArray] addObject:a]];
   [[b myMutableArray] count]; // count == 1 here but when i go back to objectB implementation it will be 0

}

and the app crash here

any ideas?

thanks,

edit: crash is gone i just edit the init method of objectA but myMutableArray still 0

in objectB.m

- (id)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {

        UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                             target:self 
                                                                             action:@selector(addBarButton:)];
        [navItem setRightBarButtonItem:bbi];
        [bbi release];

        myMutableArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)addBarButton:(id)sender {
    myObjectC = [[objectC alloc] init];
    [self presentModalViewController:myObjectC animated:YES];

}

i also have in objectB.h

@property (nonatomic, retain) NSMutableArray *myMutableArray;

the count of myMutableArray in objectC in saveButtonPressed method is now 1 but when i back to objectB tableView where i want to display the myMutableArray is still 0

Edit2: after i put a lot of NSLog i found that when i make new objectB in saveButtonPressed: method the count of myMutableArray will be 1 but when i go back to my tableView (objectB) myMutableArray is back to 0 maybe because i create new and separate object of objectB in objectC (saveButtonPressed:) also if i don't alloc and init the objectB in saveButtonPressed: method objectB will be nil and i cant put objectA in myMutableArray

so i guess i have to get pointer to original objectB but how?

Since count == 0 after you added an object, I'm guessing it's one of two things:

  1. [b myMutableArray] is returning nil because you forgot to allocate myMutableArray in the init function for objectB (eg myMutableArray = [[NSMutableArray alloc] initWithCapacity:10]; )

  2. [b myMutableArray] is returning a new mutable array each time it's called. Perhaps you are doing something like:

- (NSMutableArray *)myMutableArray {
  return [NSMutableArray arrayWithCapacity:10];
}

Sounds like (1) is probably the likely cause but it still doesn't explain the crashing. What error messages do you see in the debug console when the crash happens?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM