Variables looking strange in NSLog?
I'm pretty new to iphone app development, and so I'm starting off using the Cocos2d framework instead of entirely objective c from scratch, because it has lowered the learning curve some and I'm finding it much easier. I'm attempting to do some of the tutorials off the Cocos2d website, but, being human rather than a sheep, have decided to try some stuff that wasn't in the tutorial.
So, here's the relevant code from the tutorial:
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [self convertTouchToNodeSpace: touch];
[cocosGuy stopAllActions];
[cocosGuy runAction: [CCMoveTo actionWithDuration:1 position:ccp( location.x + 32, location.y + 32)]];
This works as expected, the cocos guy moves to where I touch my ipod with a one second delay and a 32 pixel offset. So now, I wanted to get some textual feedback as well about where I was tapping the screen. I tried to use NSLog to display some information about the location
of the touch:
NSLog([NSString stringWithFormat:@"x=%d y=%d", location.x, location.y]);
Xcode kindly informs me: Format not a string literal and no format arguments
. Unless I'm entirely mistaken, @"text"
is a string literal in objective C and the two location variables are the format arguments.
When run, the console displays something like:
2011-06-20 19:54:22.765 Lesson1[315:707] x=0 y=1078640640
2011-06-20 19:54:23.887 Lesson1[315:707] x=0 y=1080631296
2011-06-20 19:54:24.576 Lesson1[315:707] x=0 y=1080242176
when I touch the ipod screen at three random places. x never seems to change and y is always a massive integer.
What is wrong with my code, and being new to the world of iphone development, objective c, and cocos2d, how can I go about trying to better debug this myself in the future?
Please note that this is literally my sec开发者_C百科ond day of experimenting with all of this, so forgive me if this is an incredibly obvious or stupid question.
The x, y
members of CGPoint
are float
s, not int
s, hence you should use %f
instead of %d
.
Also, NSLog()
is able to do formatting itself. Try:
NSLog(@"x=%f y=%f", location.x, location.y);
instead. Note that in the code above the first parameter to NSLog()
:
@"x=%f y=%f"
is a string literal, whilst in your code the first parameter is not literal:
[NSString stringWithFormat:@"x=%d y=%d", location.x, location.y]
It looks like you’re using the wrong string format specifiers.
%d
is for an int
, but the x
and y
attributes of a CGPoint
struct are actually CGFloat
(which is defined as either a float
or double
, depending on 32 or 64 bit)
You can see Apple’s list of format specifiers here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html%23//apple_ref/doc/uid/TP40004265
It says CGFloats should use %f
or %g
instead.
To make +stringWithFormat:
interpret the values correctly, your format string should look like @"x=%f y=%f"
Another tip – NSLog() actually has formatting built in, so you can just use
NSLog(@"x=%f y=%f", location.x, location.y);
instead of also [NSString stringWithFormat:…
The reason for the warning is that NSLog
expects its first argument to be a string literal, and you are passing it an object that you've just created. Also, I'm pretty sure location.x
and location.y
will be floating point numbers, not integers. Try using %f
instead of %d
. At any rate, you should use NSLog
to format the string instead, like so:
NSLog(@"x=%f y=%f", location.x, location.y);
精彩评论