NSArray导致viewDidLoad崩溃

时间:2012-01-16 21:33:41

标签: ios5 crash nsarray viewdidload

我有一个应用程序,它有两个独立的视图,两个独立的(和单独填充的)NSArrays。在array1中,我有10个@“ABC”对象,而在array2中,我有18个@“ABC”对象。带有array1的view1加载完全正常;但是,view2崩溃了。我在array2中更改了@“ABC”项的数量,作为一种试错的方式进行调试,发现我只能拥有15个“ABC”对象。一旦我添加了第十六个@“ABC”,应用程序就崩溃说了viewDidLoad。有谁知道如何解决这个或我正在做的那会导致应用程序崩溃?

- (void)viewDidLoad {
    array2 = [[NSArray alloc] initWithObjects:@"ABC1", @"ABC2", @"ABC3", @"ABC4", @"ABC5", @"ABC6", @"ABC7", @"ABC8", @"ABC9", @"ABC10", @"ABC11", @"ABC12", @"ABC13", @"ABC14", @"ABC15", ABC16", @"ABC17",@"ABC18",nil];

    [super viewDidLoad];
}

(#pragma mark Table view methods)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [array2 count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Set up the cell...
    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor redColor];

    return cell;
}

就像我说的,array2可以很好地处理15个或更少的对象,但是一旦我添加了16或更多,它就会崩溃。

1 个答案:

答案 0 :(得分:0)

从你的问题:

array2 = [[NSArray alloc] initWithObjects:@"ABC1", @"ABC2", @"ABC3", @"ABC4", @"ABC5", @"ABC6", @"ABC7", @"ABC8", @"ABC9", @"ABC10", @"ABC11", @"ABC12", @"ABC13", @"ABC14", @"ABC15", ABC16", @"ABC17",@"ABC18",nil];

NSArrays只能包含对象,而不能包含原始C类型。您的第16个元素ABC16"将在array2初始化期间作为基元读入并使您的应用崩溃。

ABC16"应为@"ABC16"

相关问题