2012-07-12 52 views
1

我是iPhone dev的新手,所以這可能看起來像一個非常愚蠢的問題,但是如何將計算值保存到核心數據?將計算值鑄造到小數點並保存到核心數據

背景 該程序只是從用戶輸入到文本字段中計算出一個值。我有他們的數據類型作爲NSStrings,並保存到核心數據時,一切正常。 我正在將NSStrings轉換爲浮點數並執行相應的數學運算以獲得最終的計算值。該最終值不會輸出到文本字段,而是保存在覈心數據中。我一直在同一行發現這兩個錯誤:*「將'double'傳遞給不兼容類型'NSString '的參數和」從不兼容類型'double'指派給'NSString *'。 (第10行是一個與這些消息)

​​

*右之前我張貼此代碼,我試圖改變線7到

NSString finalPrice = totalPrice/totalPercentage;

這引起了上述第10行錯誤走開,但在第7行上導致錯誤:'接口類型不能靜態分配。' 這一切都讓人困惑。我不明白爲什麼我的所有其他文本字段都保存在覈心數據中,但是這個計算出來的數據不會。 任何幫助你們可以給我將不勝感激。 :)

編輯 所以我現在實際上是由我建立的NSLog(@"%@", dataModel.finalPrice);得到一個號碼,但價值wayyyyy關閉。它似乎並不彷彿數學被正確完成:

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; 

我添加了這些NSLogs,看看有什麼被保存到核心數據

NSLog(@"%@", dataModel.price); 
NSLog(@"%@", dataModel.percentage);  
NSLog(@"%@", dataModel.price2); 
NSLog(@"%@", dataModel.percentage2); 
NSLog(@"%@", dataModel.finalPrice); 

似乎一切都保存好,但對於例如,如果我有:

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 

但是,我輸出到NSLog的值顯示爲-2147483648。 世界上哪裏來的?

編輯 我得救了!這是我做的:

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; 

哇。這是一個愚蠢的。我無法相信最終的解決方案如此簡單! 再次感謝您的幫助!

+0

在你的第一個例子中,你正在使用'float'值,但是在你的第二個例子中你正在使用'int'。你應該堅持使用'float',併爲此提供NSLog輸出。 – mopsled 2012-07-14 00:14:37

+0

Okie dokie。 因此,用於finalPrice的NSLog返回'null',這導致NSLog for finalFinalPrice返回'NaN' – MightyMorphinHipsterRanger 2012-07-14 17:32:22

回答

1

如果你想一個float值存儲爲NSString,使用stringWithFormat:

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

dataModel.finalPrice = finalPriceString; 

編輯

您的修正:

NSString finalPrice = totalPrice/totalPercentage; 

有一些問題。首先,錯誤Interface type cannot be statically allocated來自NSString需要動態分配(作爲指針)的事實,並且您沒有使用指針類型(NSString *)。

其次,totalPricetotalPercentagefloat的價值觀和你分割併爲其設置一個NSString,這是一個非常不同類型的數據。

+0

THANKYOU THANKYOU THANKYOU! :D – MightyMorphinHipsterRanger 2012-07-13 16:17:30

+0

好的。好像我跳了一下槍。 儘管我不再收到任何錯誤消息,但計算的值未顯示在我的應用的另一個區域的另一個textField中。 我用'NSLog(@「%@」,[finalPrice text]);'看看是否有任何東西被保存,並且事實證明finalPriceString沒有被保存在覈心數據中,儘管其他的東西都是。 你有什麼想法,爲什麼現在可能會發生? – MightyMorphinHipsterRanger 2012-07-13 19:16:00

+0

你在'finalPrice'的數據模型中發送了什麼類型? – mopsled 2012-07-13 19:54:10