我有一个UILabel,我需要从整数中获取值,这样我就可以将它保存到我的Core Data对象中,但此时我一直收到此错误和失败。
PurchaseOrderItem *newPOItem = (PurchaseOrderItem*) [NSEntityDescription insertNewObjectForEntityForName:@"PurchaseOrderItem" inManagedObjectContext:managedObjectContext];
int qty = [qtyTextField.text integerValue];
[newPOItem setProductName:productName.text];
[newPOItem setDescription:productDescription.text];
[newPOItem setPrice:[NSDecimalNumber decimalNumberWithString: retailPrice.text]];
[newPOItem setQuantity:qty];
[newPOItem setPurchaseorder:newEntity];
if (![managedObjectContext save:&error]) {
// Handle the error.
NSLog(@"%@",error);
}
编辑: 这是我的最终解决方案 - 看起来非常荒谬但是有效:
NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString: [retailPrice.text stringByReplacingOccurrencesOfString:@"$" withString:@""]];
NSNumber *qty = [NSNumber numberWithInt: [qtyTextField.text integerValue]];
NSDecimalNumber *itemSubTotal =[price decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithDecimal:[qty decimalValue]]];
答案 0 :(得分:3)
我愿意打赌PurchaseOrderItem
的数量属性声明为NSNumber
而不是NSInteger
。如果是这种情况,您需要执行类似以下操作:
[newPOItem setQuantity: [NSNumber numberWithInt: qty]];