1

我收到 NSArray 的 EXC_BAD_ACCESS 错误。还有其他阵列,它们工作正常。我在 viewDidLoad 中初始化了数组。每当我从不同的块访问它时,我都会收到错误消息。但是这个数组是在头文件中定义的。ARC 已开启。这是我的代码

头文件.h

@interface PopoverViewController : UITableViewController 
{ 
    NSArray *typeFilterItem; 
    NSArray *changeFilterItem; 
    NSArray *nFragmentFilterItems; 
}

.m 文件

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    // set section
    typeFilterItem = [NSArray arrayWithObjects:@"All",
                      @"Type-1",
                      @"Type-2",
                      @"Type-3", nil];
    changeFilterItem = [NSArray arrayWithObjects:@"All",
                        @"Static Change",
                        @"Consistent Change",
                        @"Inconsistent Change", nil];


    nFragmentFilterItems = [NSArray arrayWithObjects:@"Min",
                            @"Max", nil]; // this is the array causing problem
    NSLog(@"count: %d", nFragmentFilterItems.count); // here its ok
    // set filters
    [self setAllTypeFilers];
    [self setAllChangePatternFilters];

}

在合适的数据源中

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    /******* getting error at this line *****/
    NSLog(@"count: %d", nFragmentFilterItems.count);
    if (section == 0) {
        return typeFilterItem.count;
    } else if (section == 1) {
        return changeFilterItem.count;
    } else if (section == 2) {
        return 2;
    } else if (section == 3) {
        return 1;
    }
    return 0;
}

提前致谢

4

2 回答 2

1

如果在您的头文件中,您有:

@property(nonatomic, strong)NSArray *nFragmentFilterItems;

你必须打电话:

self.nFragmentFilterItems = [NSArray arrayWithObjects:@"Min", @"Max", nil];

您还必须在实现文件中综合您的属性:

@synthesize nFragmentFilterItems;
于 2012-11-12T21:48:39.637 回答
1

请试试这个

nFragmentFilterItems = [[NSArray arrayWithObjects:@"Min",
                            @"Max", nil] retain];

Tks

于 2012-11-14T13:57:03.247 回答