传递'setQuantity:'的参数1使得整数指针没有强制转换

时间:2010-07-12 20:50:18

标签: iphone objective-c ipad

我有一个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]]]; 

1 个答案:

答案 0 :(得分:3)

我愿意打赌PurchaseOrderItem的数量属性声明为NSNumber而不是NSInteger。如果是这种情况,您需要执行类似以下操作:

[newPOItem setQuantity: [NSNumber numberWithInt: qty]];