0
singelton.categoryId = (int)[categories.categoriesId objectAtIndex:indexPath.row];
NSLog(@"%d", singelton.categoryId);

singelton.categoryId来自 type int
但是当我尝试打印它时,数字是随机的,但如果我这样做NSLog(@"%@", singelton.categoryId);,打印的值是正确的。
我需要用 打印它%d

有人能帮我吗?

4

6 回答 6

4

使用intValue方法获取NSString/NSNumber的整数表示。尝试,

singelton.categoryId = [[categories.categoriesId objectAtIndex:indexPath.row] intValue];
于 2011-07-22T08:28:15.773 回答
2

我假设数组返回一个 NSNumber。所以试试这个。

int catId = [ ((NSNumber*) [categories.categoriesId objectAtIndex:indexPath.row] ) intValue];
NSLog(@"%d, catId )
于 2011-07-22T08:30:49.063 回答
1

尝试以下操作:

NSLog(@"%d", [singelton.categoryId intValue]);
于 2011-07-22T08:28:56.193 回答
1

尝试这样做:

singelton.categoryId = [[categories.categoriesId objectAtIndex:indexPath.row] intValue]

这是因为您不能将整数存储在数组中 - 它们必须是NSNumbers.

于 2011-07-22T08:29:11.953 回答
1

返回的类别 IDobjectAtIndex:是一个对象,很可能是一个NSNumber. 通过将其转换为int您可以获得对象的地址,而不是存储在其中的数值。正确的代码是这样的:

singleton.categoryID = [[… objectAtIndex:…] intValue];
于 2011-07-22T08:29:53.123 回答
1

当它是一个适当的对象时,它不可能来自int原始类型(假设它objectAtIndex在这里的行为与 Objective-C 中的其他地方一样——即,它不是你自己的方法。所以,你需要询问intValue

[[categories.categoriesId objectAtIndex:indexPath.row] intValue]

如果它是可用的方法。什么课categoriesID

于 2011-07-22T08:32:22.593 回答