NSMutableArray 不存储任何对象

发布于 2024-12-21 04:20:50 字数 1216 浏览 4 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(5

梦归所梦 2024-12-28 04:20:50

也许那个特定的 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.

浪荡不羁 2024-12-28 04:20:50

-initWithNibName:bundle:UIViewController 指定的初始化程序,因此任何初始化代码都应该显示在该方法的重写中。如果此UITableViewController的子类,那么Mark认为initWithStyle是放置它的好地方是正确的。

另外,一般来说,viewDidLoad/viewDidUnload 为您提供了一个分配/释放视图所需数据结构的好地方,但如果需要,可以重新创建这些数据结构。

-initWithNibName:bundle: is UIViewController's designated initializer, so any init code should show up in an override of that method. If this is a subclass of UITableViewController, Mark is correct that initWithStyle 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.

两个我 2024-12-28 04:20:50

尝试以这种方式初始化你的 NSMutableArray:

self.selectedWorkCenters = [[NSMutableArray alloc] init];

Try initializing your NSMutableArray in this way:

self.selectedWorkCenters = [[NSMutableArray alloc] init];
疯了 2024-12-28 04:20:50

好的,这是我的想法。在您的 init 中,您正在使用:

// This one is wrong, you are [NSMutableArray array] returns a NSArray which cannot be 
// mutated so you cannot add objects to it. That is why it is not working.
//
self.selectedWorkCenters = [NSMutableArray array];

// This one is wrong because you have a leak here (Although it should work)
//
self.selectedWorkCenters = [[NSMutableArray alloc] init];

尝试一下:

selectedWorkCenters = [[NSMutableArray alloc] init];

并将其放在 viewDidLoad 方法中。希望有帮助。让我知道:)

Ok so here are my ideas. In your init you are using:

// This one is wrong, you are [NSMutableArray array] returns a NSArray which cannot be 
// mutated so you cannot add objects to it. That is why it is not working.
//
self.selectedWorkCenters = [NSMutableArray array];

// This one is wrong because you have a leak here (Although it should work)
//
self.selectedWorkCenters = [[NSMutableArray alloc] init];

Try this:

selectedWorkCenters = [[NSMutableArray alloc] init];

And place it in the viewDidLoad method. Hope it helps. Let me know :)

夏天碎花小短裙 2024-12-28 04:20:50

我已经找到了错误。

在另一种方法中,我的分配

selectedWorkCenters = availableWorkCenters;

availableWorkCenters 未初始化 - 在调试器屏幕中始终为 0x0。

I have found the error.

In a other method I had the assignment

selectedWorkCenters = availableWorkCenters;

availableWorkCenters wasn't initialized - always 0x0 in the debugger screen.

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