Very strange -% I did not work for integers,% d did?

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

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

gave me strFriendID as an empty string.

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

gave me strFriendID as 1 .

How can this happen?

+6
source share
2 answers

I don’t know why you got 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 give me 1

+2
source

By Apple :

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

So it’s strange that% I didn’t work, but it’s not so strange that% d worked.

Perhaps btnTemp.tag was null at this point

+1
source

Source: https://habr.com/ru/post/911380/


All Articles