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

singelton.categoryId is from type int.
But when I try to print it number is random, but if I do this NSLog(@"%@", singelton.categoryId); the printed value is right.
I need to print it with %d.

Can someone help me?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
nyanev
  • 11,299
  • 6
  • 47
  • 76

6 Answers6

4

Use intValue method to get the integer representation of a NSString/NSNumber. Try,

singelton.categoryId = [[categories.categoriesId objectAtIndex:indexPath.row] intValue];
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
2

I am assuming the array returns you an NSNumber. So try this out.

int catId = [ ((NSNumber*) [categories.categoriesId objectAtIndex:indexPath.row] ) intValue];
NSLog(@"%d, catId )
Vidyanand
  • 967
  • 8
  • 14
1

try the following:

NSLog(@"%d", [singelton.categoryId intValue]);
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
1

Try do do this in this way:

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

It's because you can't store ints in array - they must be NSNumbers.

Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
akashivskyy
  • 44,342
  • 16
  • 106
  • 116
1

The category ID as returned by objectAtIndex: is an object, most probably an NSNumber. By casting it to int you get the address of the object, not the numeric value stored in it. The correct code would be something like this:

singleton.categoryID = [[… objectAtIndex:…] intValue];
zoul
  • 102,279
  • 44
  • 260
  • 354
1

There's no way that's coming from an int primitive type when it's a proper object (assuming that objectAtIndex behaves here as elsewhere in Objective-C--i.e., it's not your own method. So, you'll need to ask for the intValue:

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

if it is an available method. What class is categoriesID?

GarlicFries
  • 8,095
  • 5
  • 36
  • 53