字符串格式返回指针值,而不是数字

时间:2021-02-08 06:55:07

I am having an issue where I am trying to take an NSInteger, and place the value into a label. Here is the code:

我有一个问题,我试图采取NSInteger,并将值放入标签。这是代码:

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

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

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

}


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

Item list has one item with a Quantity of 4, however, the label is showing the pointer reference number, not the value of 4. What am I missing?

物品清单有一个数量为4的物品,但是,标签显示的是指针参考号,而不是4的值。我缺少什么?

Thanks in advance for your help!

在此先感谢您的帮助!

3 个解决方案

#1


1  

Try this :

试试这个 :

NSInteger itemCount = 0;

You didn't tell it you wanted to start counting from 0 :)

你没有告诉它你想从0开始计数:)

#2


0  

Does it help to change the NSInteger to int

将NSInteger更改为int是否有帮助?

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

#3


0  

Replace

NSInteger itemCount;

with

int itemCount;

#1


1  

Try this :

试试这个 :

NSInteger itemCount = 0;

You didn't tell it you wanted to start counting from 0 :)

你没有告诉它你想从0开始计数:)

#2


0  

Does it help to change the NSInteger to int

将NSInteger更改为int是否有帮助?

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

#3


0  

Replace

NSInteger itemCount;

with

int itemCount;