添加对象后 NSMutableArray 的计数为 0

发布于 2024-11-11 17:34:17 字数 2085 浏览 0 评论 0原文

你好 我有一个对象(objectA),有两个实例变量,NSString 和 NSDate。 我还有另外两个物体 一个带有 tableView 和添加按钮(objectB) 当您按下添加按钮(objectC)时,会以模态方式呈现一个,在此对象视图中,您可以键入名称和日期,当该对象(objectC)关闭时,我会创建带有名称和日期的新对象(objectA)。

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

}

应用程序崩溃了,

有什么想法吗?

谢谢,

编辑: 崩溃消失了我只是编辑 objectA 的 init 方法 但 myMutableArray

在 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];

}

仍然为 0我也在 objectB.h 中

@property (nonatomic, retain) NSMutableArray *myMutableArray;

在 saveButtonPressed 方法中的 objectC 中 myMutableArray 的计数现在为 1 但是当我回到 objectB tableView 我想显示 myMutableArray 的地方仍然是 0

Edit2 : 在我放了很多 NSLog 之后 我发现当我在 saveButtonPressed: 方法中创建新的 objectB 时,myMutableArray 的计数将为 1 但是当我回到我的 tableView (objectB) myMutableArray 又回到 0 也许是因为我在 objectC 中创建了新的单独的 objectB 对象(saveButtonPressed:) 还 如果我不在 saveButtonPressed: 方法中分配并初始化 objectB objectB 将为零,我无法将 objectA 放入 myMutableArray

所以我想我必须获取指向原始 objectB 的指针,但是如何获取?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-11-18 17:34:17

由于添加对象后 count == 0,我猜测这是以下两件事之一:

  1. [b myMutableArray] 返回 nil 因为您忘记在 objectB 的 init 函数中分配 myMutableArray (例如 myMutableArray = [[NSMutableArray alloc] initWithCapacity:10];

  2. [b myMutableArray] 每次调用时都会返回一个新的可变数组。也许您正在做类似的事情:

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

听起来(1)可能是可能的原因,但它仍然不能解释崩溃。发生崩溃时,您在调试控制台中看到什么错误消息?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文