0

I have this code:

likeButton.tag = spik._id;
NSLog(@"spik ID=%@   likebtn.tag=%@",spik._id,likeButton.tag);
NSLog(@"%@", likeButton);

where spik._id is NSInteger

And this is the output:

2012-03-11 19:35:37.751 KwikSpik[44277:ce03] spik ID=39975   likebtn.tag=39975   
2012-03-11 19:35:37.752 KwikSpik[44277:ce03] <<UIButton: 0xd178b60; frame = (20 12604; 30 30); opaque = NO; tag = 90793136; layer = <CALayer: 0xd1788d0>>

You see that likeButton.tag = 39975 here but when I log the button, its tag is 90793136.

if I write NSLog(@"spik ID=%@ likebtn.tag=%d",spik._id,likeButton.tag);

then I get

012-03-11 23:27:38.290 KwikSpik[45326:ce03] spik ID=39975 likebtn.tag=87582784

Are these different presentations of one number or different values? Why are these values different?

Later if I write

NSLog(@"tag d %d",sender.tag);
NSLog(@"tag @ %@",sender.tag);

where sender is likeButton then first NSLog outputs 87582784 and second crashes with EXC_BAD_ACCESS (code = 1, address = 0x30...)

What's happening? Why there're different values in likeButton.tag and spik._id - or they just look like different?

Why NSLog first time outputted likeButton.tag with %@ and the second time it crashed?

Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84

2 Answers2

1
NSLog(@"spik ID=%@ likebtn.tag=%d", spik._id, likeButton.tag);

When you log your button, tag is not considered as an int.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
janusfidel
  • 8,036
  • 4
  • 30
  • 53
1

They are being interpreted differently by NSLog as you used %@. Use %d to output as an integer.

NSLog(@"spik ID=%d   likebtn.tag=%d", spik._id,likeButton.tag);
Thomas Nadin
  • 1,167
  • 10
  • 22