views:

70

answers:

1

I have a UILabel that I need to get the value from as an integer so I can save it to my Core Data object but I keep getting this error and failure at this point.

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

EDIT: Here is my final solution - seems totally ridiculous but it works:

NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString: [retailPrice.text stringByReplacingOccurrencesOfString:@"$" withString:@""]];
NSNumber *qty = [NSNumber numberWithInt: [qtyTextField.text integerValue]];
NSDecimalNumber *itemSubTotal =[price decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithDecimal:[qty decimalValue]]]; 
+3  A: 

I'm willing to bet that the quantity attribute of PurchaseOrderItem is declarated as NSNumber not NSInteger. If that's the case you'll need to do something similar to the following:

[newPOItem setQuantity: [NSNumber numberWithInt: qty]];
Bryan Kyle
that was it, man learning all of the different ways to work with strings and numbers is confusing in objective-c - thank you!
Slee
now I have a NSDecimalNumber and a NSNumber and I need to multiply those 2 - thoughts?
Slee
I'm not familiar with NSDecimalNumber. Is there a reason you're using that instead of NSNumber with floats? If you were using NSNumbers you could just do that following:NSNumber result = [NSNumber numberWithFloat: [a floatValue] * [b floatValue]];
Bryan Kyle