1

I am making a (sort of) trading card game , using SpriteKit. I created a Card class, and each card has a rank :

// in Card.h    
@property NSInteger cardRank;

In one of my another class (Game class), i'm trying to retrieve this value. I create a Card instance, and display the value in the console (testing purpose) :

Card *tmpCard = [[Card alloc] init];
NSLog(@"%@", tmpCard.cardRank);

When I use %@ in the NSLog, I get the right value for the cardRank, but an Xcode warning saying that "Values of type nsinteger should not be used as format arguments" and that I should cast to "long".

If I cast to long… :

NSLog(@"%ld", (long)tmpCard.cardRank);

… I got no error, but not the right value for cardRank (it displays something like "140378469207968").

Could someone explain me why I got this result ?
I am probably making a rookie mistake but couldn't understand it myself in the las few days.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Have you assigned it a default value? – Parth Bhatt Apr 21 '15 at 09:19
  • "When I use %@..." Are you sure you used %@ without a crash? – gnasher729 Apr 21 '15 at 09:19
  • @ParthBhatt: Everything in an Objective-C object is initialised to zeroes. – gnasher729 Apr 21 '15 at 09:20
  • %@ didn't crash with an integer? I find that hard to believe. Please check this carefully. When I type %@ I mean %@ and not whatever you used in your code. Others noticed that your post is quite carelessly written. – gnasher729 Apr 21 '15 at 09:29
  • 2
    Hi, Please do not deface your post after you have taken help from it. It is like cutting down a tree after taking shelter below it. Please allow the other future users to gain from the knowledge. The answerers would have put a lot of effort. Do not put their valuable time to waste. – Bhargav Rao Jan 03 '16 at 09:00
  • Considering the top answer is "get a book", I don't see 1. the effort put in this answer, 2. the utility for other people. Plus everyone agree that the post is useless and carelessly written. –  Jan 03 '16 at 13:14

4 Answers4

2

Get a good book about the C language, and look at format strings. The format strings in Objective-C are exactly the same, except for %@ which expects an object.

The correct way to print NSInteger and NSUInteger is %zd (which works fine on 32 and 64 bit). Anyway, if you turn warnings on in Xcode (as you should) and if you turn warnings into errors in Xcode (as you should), the compiler will tell you where you are wrong and even make suggestions how to fix it.

BTW. If you use %@ to print an NSInteger, I expect a crash. Your post doesn't seem to contain the truth. When you have questions, report very, very precisely and correctly what you are doing. 'It displays something like "140378469207968"' is useless. Show exactly what it displays.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • "he correct way to print NSInteger and NSUInteger is %zd" It's certainly not the "correct" way to print `NSUInteger`, which is unsigned. – newacct Apr 22 '15 at 01:12
2

Log int with %d

Log NSInteger with %zd

Log NSUInteger with %tu

SteBra
  • 4,188
  • 6
  • 37
  • 68
0

You can find String Format Specifiers on Apple doc.

BTW, NSLog(@"%ld", (long)tmpCard.valeurTop); will for sure not show you "the right value for cardRank" as you're asking for valeurTop...

0

%d format specifier is used for NSInteger type values but since iOS supports both 32 & 64 bits, 64 bit NSInteger is of type long, which can be printed with %ld format specifier.

For a reference %@ is used for NSString values.

BTW you are printing the wrong variable hence it prints garbage value.

atulkhatri
  • 10,896
  • 3
  • 53
  • 89