NSMutableArray 不存储任何对象
我的 NSMutableArray 不存储任何对象,无论是字符串还是复杂的类。我尝试分配 ind init 并将 init 作为数组。两者都没有成功。
.m 文件:
@interface WorkCentersViewController()
@property (nonatomic, retain) NSMutableArray *selectedWorkCenters;
@end
@implementation WorkCentersViewController
@synthesize selectedWorkCenters;
-(id)init{
self = [super init];
if(self){
//self.selectedWorkCenters = [[NSMutableArray alloc] init]; //doesn't work too
self.selectedWorkCenters = [NSMutableArray array];
}
return self;
}
- (void)dealloc
{
[selectedWorkCenters release];
[super dealloc];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
...
}else{
//select
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//add
[self.selectedWorkCenters addObject:[self.workCenters objectAtIndex:indexPath.row]];
NSLog(@"Sel WC Count: %d", [self.selectedWorkCenters count]); //always 0
}
}
为什么它不存储我的 WorkCenter 对象?我做错了什么?
BR, 迈贝克斯
My NSMutableArray doesn't store any object, either a String or a complex class. I tried it to allocate ind init and to init as an array. Both without success.
.m file:
@interface WorkCentersViewController()
@property (nonatomic, retain) NSMutableArray *selectedWorkCenters;
@end
@implementation WorkCentersViewController
@synthesize selectedWorkCenters;
-(id)init{
self = [super init];
if(self){
//self.selectedWorkCenters = [[NSMutableArray alloc] init]; //doesn't work too
self.selectedWorkCenters = [NSMutableArray array];
}
return self;
}
- (void)dealloc
{
[selectedWorkCenters release];
[super dealloc];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
...
}else{
//select
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//add
[self.selectedWorkCenters addObject:[self.workCenters objectAtIndex:indexPath.row]];
NSLog(@"Sel WC Count: %d", [self.selectedWorkCenters count]); //always 0
}
}
Why is it not storing my object of WorkCenter? What I'm doing wrong?
BR,
mybecks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许那个特定的 init 方法没有被调用?这是 UITableViewController 吗?也许您正在使用
initWithStyle:
初始化它?我会在初始化数组的行上设置一个断点以进行检查。Perhaps that specific init method isn't being called? Is this a UITableViewController? Maybe you're initializing it with
initWithStyle:
? I'd set a break point on the line where you init the array just to check.-initWithNibName:bundle:
是UIViewController
指定的初始化程序,因此任何初始化代码都应该显示在该方法的重写中。如果此是UITableViewController
的子类,那么Mark认为initWithStyle
是放置它的好地方是正确的。另外,一般来说,viewDidLoad/viewDidUnload 为您提供了一个分配/释放视图所需数据结构的好地方,但如果需要,可以重新创建这些数据结构。
-initWithNibName:bundle:
isUIViewController
's designated initializer, so any init code should show up in an override of that method. If this is a subclass ofUITableViewController
, Mark is correct thatinitWithStyle
is a good place to put it.Also, in general,
viewDidLoad
/viewDidUnload
give you a good place to alloc/dealloc data structures that your view needs, but that can be recreated if needed.尝试以这种方式初始化你的 NSMutableArray:
Try initializing your NSMutableArray in this way:
好的,这是我的想法。在您的 init 中,您正在使用:
尝试一下:
并将其放在 viewDidLoad 方法中。希望有帮助。让我知道:)
Ok so here are my ideas. In your init you are using:
Try this:
And place it in the viewDidLoad method. Hope it helps. Let me know :)
我已经找到了错误。
在另一种方法中,我的分配
availableWorkCenters 未初始化 - 在调试器屏幕中始终为 0x0。
I have found the error.
In a other method I had the assignment
availableWorkCenters wasn't initialized - always 0x0 in the debugger screen.