简体   繁体   English

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

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

I have four textfields like textfield1 , textfield2 , textfield3 , textfield4 and one button. 我有四个文本textfield1 ,例如textfield1textfield2textfield3textfield4和一个按钮。 When I click on the button I am adding the textfield text to an NSMutableArray and populating in a tableview . 当我单击按钮时,我正在将文本字段文本添加到NSMutableArray并填充在tableview

My code is: 我的代码是:

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

Populating in table view using delegate methods 使用委托方法在表格视图中填充

- (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;
}

The above code is working fine. 上面的代码工作正常。 But when I click on button without entering any data in the textfields, the app is crashing because the array is empty. 但是,当我单击按钮时未在文本字段中输入任何数据时,由于数组为空,应用程序崩溃。 How to solve this problem? 如何解决这个问题呢?

You can't add nil to an array. 您不能将nil添加到数组中。

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

This makes sure there are four items in the array (possibly empty strings). 这样可以确保数组中有四个项目(可能是空字符串)。

Why crashes? 为什么会崩溃?

You can't add nil in array. 您不能在数组中添加nil。 It should have something inside of it. 它里面应该有东西。

-(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];
    }



}

Try this 尝试这个

-(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];

 }

Populating in table view using delegate methods 使用委托方法在表格视图中填充

- (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;
}

if you have multiple text fields then: //give tag value of each textfield from 100 to 150 or any any tag (i am considering 50 text fields) 如果您有多个文本字段,则://给出每个文本字段的标签值从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
        }
    }
}

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

This line in the buttonClick method defined a local array variable: buttonClick方法中的这一行定义了一个本地数组变量:

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

It should be 它应该是

array =[[NSMutableArray alloc]init];

Which is your instance variable array. 这是您的实例变量数组。

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

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