简体   繁体   English

添加 object 后 NSMutableArray 的计数为 0

[英]Count of NSMutableArray is 0 after adding object

hi i have an object (objectA) with tow instance variables, NSString and NSDate.嗨,我有一个带有两个实例变量 NSString 和 NSDate 的 object (objectA)。 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.我还有另外两个对象,一个带有 tableView 和添加按钮 (objectB),一个在您按下添加按钮 (objectC) 时以模态方式呈现,在此 object 视图中您可以输入名称和日期,当此 object (objectC) 关闭时,我创建新的object (objectA) 带有名称和日期。

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 objectB 有 NSMutableArray,我想将 objectA 添加到这个数组中,这样它就可以出现在 tableView 中,我在 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编辑:崩溃消失了我只是编辑了 objectA 的 init 方法,但 myMutableArray 仍然是 0

in objectB.m在对象B.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我也有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 saveButtonPressed 方法中 objectC 中 myMutableArray 的计数现在为 1,但是当我返回要显示 myMutableArray 的 objectB tableView 时,仍然为 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 Edit2:在我放了很多 NSLog 之后,我发现当我在 saveButtonPressed: 方法中创建新 objectB 时,myMutableArray 的计数将为 1,但是当我 go 回到我的 tableView (objectB) myMutableArray 时,可能是因为我创建了新的并且在 objectC 中分离 objectB 的 object (saveButtonPressed:) 如果我不分配并在 saveButtonPressed 中初始化 objectB:方法 objectB 将为零,我不能将 objectA 放入 myMutableArray

so i guess i have to get pointer to original objectB but how?所以我想我必须获得指向原始 objectB 的指针,但如何?

Since count == 0 after you added an object, I'm guessing it's one of two things:由于在添加 object 之后 count == 0,我猜这是两件事之一:

  1. [b myMutableArray] is returning nil because you forgot to allocate myMutableArray in the init function for objectB (eg myMutableArray = [[NSMutableArray alloc] initWithCapacity:10]; ) [b myMutableArray]返回 nil,因为您忘记在 objectB 的 init function 中分配 myMutableArray(例如myMutableArray = [[NSMutableArray alloc] initWithCapacity:10];

  2. [b myMutableArray] is returning a new mutable array each time it's called. [b myMutableArray]每次调用时都会返回一个新的可变数组。 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.听起来(1)可能是可能的原因,但它仍然不能解释崩溃。 What error messages do you see in the debug console when the crash happens?发生崩溃时,您在调试控制台中看到了哪些错误消息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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