Home > General > Turning CGPoints into NSStrings

Turning CGPoints into NSStrings

July 2, 2011

I’m the master of the good old printf debugger.  I guess I’m dating myself though, because WTF uses printf when you’ve got NSLog?

Anyway, I’m constantly dropping NSLogs when I’m messing with CGPoints.  And I first stupidly did one of these:

CGPoint killBieber = ccp(priceOfTeaInChina, imRickJamesBitch);
NSLog(@"This is your fucking point: %f",killBieber);

Of course that doesn’t work; I’m an idiot/need less blood in my caffeine system.  So I did this:

NSLog(@"This is really your stupid point: %@",killBieber);

The %@ is awesome, because it dumps the object in your string.  It works great if you have an NSString or an NSDictionary.  Here, it blows up.

My first impulse was to do something like this:

NSLog(@"This is really your stupid point: %f,%f",killBieber.x,killBieber.y);

And that works, but it’s just as ugly and inconvenient as the performer I’m referring to in that variable.  Fortunately, there’s a better way.  Check this shit out:

NSLog(@"This is really your stupid point: %@",NSStringFromCGPoint(killBieber));

It’s a touch longer, but autocomplete is your friend here.  The sweet part of this is that there’s also a CGPointFromString, which saved my ass when I had to shove a bunch of objects into an NSMutableDictionary and use points as the key.

There are a bunch more functions if you need to do the same with sizes, rectangles, and whatever other CGCrap you are dealing with.

See also: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html

Categories: General Tags: , ,
%d bloggers like this: