0

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?

3
  • this line: [[b myMutableArray] addObject:a]]; has too many square brackets :) - i can't see anything else glaringly obvious about this code, it would be very useful to see your instance variables - specifically the NSMutableArray are defined and instantiated. Commented Jun 1, 2011 at 20:39
  • 3
    Please post the actual code that triggers your problems, not a mess of myObjectXYZABC123 obfuscations. If you cannot use your actual code, then reproduce the crash in a form that you can share with us. Commented Jun 2, 2011 at 15:16
  • sorry for the obfuscations, but that is exactly how i name it in xcode except myMutableArray is tasks in my code Commented Jun 2, 2011 at 17:41

1 Answer 1

4

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 (e.g. 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?

Sign up to request clarification or add additional context in comments.

3 Comments

in objectB init method i do it like this: myMutableArray = [[NSMutableArray alloc] init]; also the crash is gone i just fix the init method of object a but myMutableArray still 0
Are you sure the objectB init method is called at all? Try putting a breakpoint there to verify. Recall that if your UITableView is in a nib file, the initWithStyle: method won't be called.
Hmm looks like we'll need to see more code before we can figure out what's going on. I'd try using gdb watch on myMutableArray to make sure that there isn't some other code path that is setting it back to nil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.