添加对象后 NSMutableArray 的计数为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于添加对象后 count == 0,我猜测这是以下两件事之一:
[b myMutableArray]
返回 nil 因为您忘记在 objectB 的 init 函数中分配 myMutableArray (例如myMutableArray = [[NSMutableArray alloc] initWithCapacity:10];
)[b myMutableArray]
每次调用时都会返回一个新的可变数组。也许您正在做类似的事情:听起来(1)可能是可能的原因,但它仍然不能解释崩溃。发生崩溃时,您在调试控制台中看到什么错误消息?
Since count == 0 after you added an object, I'm guessing it's one of two things:
[b myMutableArray]
is returning nil because you forgot to allocate myMutableArray in the init function for objectB (e.g.myMutableArray = [[NSMutableArray alloc] initWithCapacity:10];
)[b myMutableArray]
is returning a new mutable array each time it's called. Perhaps you are doing something like: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?