2012-02-03 72 views
0

我遇到了一個問題,我試圖採取NSInteger,並將值放入標籤。以下是代碼:字符串格式返回指針值,而不是數字

NSArray *itemList = [cart objectForKey:@"CartItemList"]; 

NSInteger itemCount; 
for(NSDictionary* cartItem in itemList) { 

    itemCount += [[cartItem valueForKey:@"Quantity"] integerValue]; 

} 


[totalItems setText:[NSString stringWithFormat:@"%d",itemCount]]; 

項目列表中有一項爲4的數量,然而,標籤顯示指針引用數,不是4的值,我在想什麼?

在此先感謝您的幫助!

+0

你會得到什麼,如果你NSLog每個itemCount依次?即放NSLog(@「%d」,itemCount);作爲你的'for'循環的第一行? – deanWombourne 2012-02-03 15:59:16

回答

1

試試這個:

NSInteger itemCount = 0; 

你沒有告訴它你想從0開始計數:)

+0

就是這樣。我知道我錯過了一些簡單的事情。非常感謝你! – DJH 2012-02-03 21:15:40

0

它是否有助於改變NSIntegerint

itemCount += [[cartItem valueForKey:@"Quantity"] intValue]; 
+0

NSInteger是int或long的typedef,因此應該沒有任何作用(來自文檔:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference .html#// apple_ref/doc/uid/20000018-SW16) – deanWombourne 2012-02-03 16:01:02

+0

嗯,你是對的。但我的答案建議將integerValue更改爲intValue。這是因爲他試圖用%d格式化一個NSInteger,用於整數。如果他想格式化NSInteger的%ld。 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW5 – 2012-02-03 16:06:53

+0

啊,這是完全合理的,很好的建議! (對不起:) – deanWombourne 2012-02-03 16:58:35

0

更換

NSInteger itemCount; 

int itemCount; 
+0

NSInteger是int或long的typedef,所以應該沒有效果(來自文檔:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes /Reference/reference.html#//apple_ref/doc/uid/20000018-SW16) – deanWombourne 2012-02-03 16:00:45

相關問題