Reputation:
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.
Upvotes: 1
Views: 512
Reputation: 11333
%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.
Upvotes: 0
Reputation:
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...
Upvotes: 0
Reputation: 52548
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.
Upvotes: 2