繁体   English   中英

iOS中NSMutableArray没有对象时应用崩溃

[英]App is crashing when NSMutableArray no Objects in iOS

我有四个文本textfield1 ,例如textfield1textfield2textfield3textfield4和一个按钮。 当我单击按钮时,我正在将文本字段文本添加到NSMutableArray并填充在tableview

我的代码是:

- (void)buttonClick
{
    NSMutableArray  *array =[[NSMutableArray alloc]init];
    [array addObject: textfield1.text];
    [array addObject: textfield2.text];
    [array addObject: textfield3.text];
    [array addObject: textfield4.text];
 }

使用委托方法在表格视图中填充

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text =[array objectAtIndex:indexPath.row];
    return cell;
}

上面的代码工作正常。 但是,当我单击按钮时未在文本字段中输入任何数据时,由于数组为空,应用程序崩溃。 如何解决这个问题呢?

您不能将nil添加到数组中。

[array addObject:textfield1.text ?: @""];
[array addObject:textfield2.text ?: @""];
[array addObject:textfield3.text ?: @""];
[array addObject:textfield4.text ?: @""];

这样可以确保数组中有四个项目(可能是空字符串)。

为什么会崩溃?

您不能在数组中添加nil。 它里面应该有东西。

-(void)buttonClick
{
    NSMutableArray  *array =[[NSMutableArray alloc]init];

    if ([textfield1.text length]>0) {
        [array addObject: textfield1.text];

    }
    if ([textfield2.text length]>0) {
        [array addObject: textfield2.text];

    }
    if ([textfield3.text length]>0) {
        [array addObject: textfield3.text];
    }



}

尝试这个

-(void)buttonClick
{
    NSMutableArray  *array =[[NSMutableArray alloc]init];
     if(textfield1.text.length > 0)
        [array addObject: textfield1.text];

     if(textfield2.text.length > 0)
        [array addObject: textfield2.text];

     if(textfield3.text.length > 0)
        [array addObject: textfield3.text];

    if(textfield4.text.length > 0)
        [array addObject: textfield4.text];

 }

使用委托方法在表格视图中填充

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text =[array objectAtIndex:indexPath.row];
    return cell;
}

如果您有多个文本字段,则://给出每个文本字段的标签值从100到150或任何标签(我正在考虑50个文本字段)

for (int i=100; i<=150; i++) {
// self.view or use ur view on which u r addding textfield
    id txtF = [self.view viewWithTag:i]; 
    if([txtF isKindOfClass:[UITextField class]]) {
        UITextField *txtField = (UITextField*)txtF;
        if(txtField.text.length > 0) {
            //Add ur object
        }
    }
}

您可以先检查文本字段,然后再放置是否有文本的数组,如果是,则仅在数组中放置文本。

buttonClick方法中的这一行定义了一个本地数组变量:

NSMutableArray  *array =[[NSMutableArray alloc]init];

它应该是

array =[[NSMutableArray alloc]init];

这是您的实例变量数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM