2012-02-03 71 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

相关问题