I’m new to iPhone dev, so this probably seems like a really silly question, but how can I get a calculated value saved to core data?
Background
This program simply calculates a value from what the user inputs into text fields. I have their data types as NSStrings and when saved to core data, everything works fine.
I’m casting the NSStrings to floats and perform the appropriate mathematical operations in order to come up with the final calculated value. This final value is not output to a text field, but is saved in core data. I keep getting these two errors on the same line: *”Passing ‘double’ to parameter of incompatible type ‘NSString ‘ and “Assigning to ‘NSString *’ from incompatible type ‘double.’ (line 10 is the one with these messages)
1 float dollar1 = [self.dataModel.price floatValue];
2 float P1 = [self.dataModel.percentage floatValue];
3 float dollar2 = [self.dataModel.price floatValue];
4 float P2 = [self.dataModel.percentage2 floatValue];
5 float totalPrice = dollar1 + dollar2;
6 float totalPercentage = (P1 + P2) / 100;
7 double finalPrice = round(totalPrice / totalPercentage);
8 //Set final price to save in core data
9 NSLog(@"setting finalPrice to core data slot");
10 dataModel.finalPrice = finalPrice;
*Right before I posted this code, I tried changing line 7 to
NSString finalPrice = totalPrice / totalPercentage;
which caused the above mentioned errors on line 10 to go away, but caused an error on line 7: ‘Interface type cannot be statically allocated.’
This is all soooo confusing. I don’t understand why all of my other text fields save into core data, but this calculated one won’t.
Any help you guys can give me would be greatly appreciated.
🙂
EDIT
So I’m now actually getting a number from the NSLog(@"%@", dataModel.finalPrice); that I set up, but the value is wayyyyy off. It doesn’t seem as though the math is being done correctly:
1 int dollar1 = [self.dataModel.price intValue];
2 int P1 = [self.dataModel.percentage intValue];
3 int dollar2 = [self.dataModel.price2 intValue];
4 int P2 = [self.dataModel.percentage2 intValue];
5 int totalPrice = dollar1 + dollar2;
6 int totalPercentage = P1 + P2;
7 int finalPrice = totalPrice / ((double)totalPercentage);
8 NSDecimalNumber *finalFinalPrice = [[NSDecimalNumber alloc]initWithDouble:finalPrice];
9 //Set final price to save in core data
10 NSLog(@"setting finalPrice to core data slot");
11 dataModel.finalPrice = finalFinalPrice;
I added these NSLogs to see what is being saved into core data
NSLog(@"%@", dataModel.price);
NSLog(@"%@", dataModel.percentage);
NSLog(@"%@", dataModel.price2);
NSLog(@"%@", dataModel.percentage2);
NSLog(@"%@", dataModel.finalPrice);
Everything seems to be saving okay, but for example, if I have:
3.62 = dollar1;
2.2 = P1;
5.30 = dollar2;
.21 = P2;
totalPrice = dollar1 + dollar2; <--this should equal 8.92
totalPercentage = P1 + P2; <--this should equal 2.41
finalPrice = totalPrice / totalPercentage; <--- this should equal 3.70
My output to NSLog, however, shows a value of -2147483648.
Where in the world is that coming from?
EDIT
I got it to save! Here’s what I did:
1 NSString dollar1 = self.dataModel.price.text;
2 NSString P1 = self.dataModel.percentage.text;
3 NSString dollar2 = self.dataModel.price.text;
4 NSString P2 = self.dataModel.percentage2.text;
5 double Dollar1 = [dollar1 doubleValue];
6 double Dollar2 = [dollar2 doubleValue];
7 double p1 = [P1 doubleValue];
8 double p2 = [P2 doubleValue];
9 double totalDollar = Dollar1 + Dollar2;
10 double totalP = p1 + p2;
11 double finalPrice = totalDollar / totalP;
NSString *finalFinalPrice = [[NSString alloc] initWithFormat:@".2f", finalPrice];
12 //Set final price to save in core data
13 NSLog(@"setting finalPrice to core data slot");
14 dataModel.finalPrice = finalFinalPrice;
Wow. This was a doozy. I can’t believe the final solution was so simple!
Thanks again for all your help!!
If you want to store a
floatvalue as anNSString, usestringWithFormat:EDIT
Your correction:
has some issues. First, the error
Interface type cannot be statically allocatedcomes from the fact thatNSStrings need to be dynamically allocated (as pointers), and you’re not using the pointer type (NSString *).Second,
totalPriceandtotalPercentagearefloatvalues and you’re dividing them and setting them to anNSString, which is a very different type of data.