错误:因未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“-[NSCFNumber isEqualToString:]:”。

3
在我的应用程序中,我将“Amount”字段的值输入到文本框中,该字段的数据类型为浮点数,然后将其插入到数据库中。当我运行我的应用程序时,它会显示以下错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:   '-[NSCFNumber isEqualToString:]:

这是我的插入数据代码:

 NSString *amt=txtTotalBill.text;
float amount = [amt floatValue];


NSString *insertData=[NSString stringWithFormat:@"insert into tbl_Bills(Amount) values ('%f')",amount];

在这一点上,应用程序出现错误:
  [label3 setText:[[BillTableArray objectAtIndex:indexPath.row]objectForKey:@"Amount"]];
2个回答

9
[[BillTableArray objectAtIndex:indexPath.row]objectForKey:@"Amount"] 返回的对象不是一个 NSString 类型。
NSString *string = [NSString stringWithFormat:@"%@", [[BillTableArray objectAtIndex:indexPath.row]objectForKey:@"Amount"]];

[label3 setText:string];

哇,真的成功了!! :) 但是为什么会发生这种情况呢?为什么它不自己转换为字符串呢? - Pawriwes
抱歉回复晚了,但返回的对象是 NSNumber。因此不能直接放置在 UILabeltext 属性中。正如您在我的示例中看到的那样,如果使用 NSNumber 的描述创建一个 NSString。您也可以使用 NSNumberFormatter - rckoenes

1

您正试图将Number设置为标签(label)的文本。 标签文本必须是字符串。 因此,您需要先将Number值转换为NSString...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接