Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very strange - %i didn't work for integer, %d did?

It was very strange when I saw this on debugging my application.

int iTag = btnTemp.tag;    //btnTemp.tag = 1
NSString *strFriendID = [NSString stringWithFormat:@"%i",iTag];  

gave me strFriendID as empty string.

int iTag = btnTemp.tag;       //btnTemp.tag = 1
NSString *strFriendID = [NSString stringWithFormat:@"%d",iTag];

gave me strFriendID as 1.

How can this happen?

like image 711
Nitish Avatar asked Mar 22 '12 06:03

Nitish


2 Answers

i don't know why you get this answer, but when i read your question i tried in my project but i get the value

UIButton *btnTemp = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btnTemp.tag = 1;
int iTag = btnTemp.tag;    //btnTemp.tag = 1
NSString *strFriendID = [NSString stringWithFormat:@"%i",iTag];  
NSLog(@"Str %@", strFriendID);
NSString *strFriendID1 = [NSString stringWithFormat:@"%d",iTag];  
NSLog(@"Str %@", strFriendID1);

Out Put

2012-03-26 10:32:02.899 Leaves[506:f803] Str 1
2012-03-26 10:32:02.901 Leaves[506:f803] Str 1

both gives me 1

like image 161
Hiren Avatar answered Oct 30 '22 05:10

Hiren


As per Apple:

 %d, %D and %i all represent Signed 32-bit integers.

So yeah it's weird that %i didn't work but it's not so weird that %d worked.

Perhaps btnTemp.tag was null at that point

like image 1
Drew Avatar answered Oct 30 '22 06:10

Drew