Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8234231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:24:27+00:00 2026-06-07T18:24:27+00:00

I’m new to iPhone dev, so this probably seems like a really silly question,

  • 0

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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:24:29+00:00Added an answer on June 7, 2026 at 6:24 pm

    If you want to store a float value as an NSString, use stringWithFormat:

    ...
    double finalPrice = round(totalPrice / totalPercentage);
    NSString *finalPriceString = [NSString stringWithFormat:@"%0.2f", finalPrice];
    
    dataModel.finalPrice = finalPriceString;
    

    EDIT

    Your correction:

    NSString finalPrice = totalPrice / totalPercentage;
    

    has some issues. First, the error Interface type cannot be statically allocated comes from the fact that NSStrings need to be dynamically allocated (as pointers), and you’re not using the pointer type (NSString *).

    Second, totalPrice and totalPercentage are float values and you’re dividing them and setting them to an NSString, which is a very different type of data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.