Today I stumbled on a nice problem in a project I’m working on. I have an array with objects where each object has a weight. I then need to pick an object randomly while considering the weights (i.e heavier objects will get picked more often). If you think about this problem a bit you’ll definitely come up with a working solution, but Eli Bendersky took the time to sum it up nicely for all of us.
Thanks Eli!
Posted Monday, March 22nd, 2010 by Ofri Wolfus in Code | No Comments »
So was trying to debug my app in the simulator when I got one of these cryptic “Error 14389654: The debugger can’t launch the simulator” messages. Obviously the simulator was already running. Fine, let’s quit the simulator and Xcode, and relaunch them. But then when I hit Command+Q in Xcode I got this frightening error:

Don’t you just love Xcode?
Posted Friday, March 19th, 2010 by Ofri Wolfus in Code | 1 Comment »
I’ve been researching Git for some time now and started using it full time. I decided to give Git a try after having a lot of pain with SVN. The experience until now has been absolutely amazing! Read the rest of this entry »
Posted Wednesday, March 17th, 2010 by Ofri Wolfus in Code, dPompa | No Comments »
As an iPhone developer you have to handle low memory situations gracefully and clear as much memory as you can. These can often lead to unexpected crashes if your memory management has a bug somewhere. If you wish to catch these easily then do the following:
- Go to your executable settings in Xcode and set the environment variable NSZombieEnabled to YES.

- Go to System Preferences > Keyboard > Keyboard Shortcuts and add a shortcut for the iPhone Simulator menu item called “Simulate Memory Warning”. I set mine to command+1.

- Add a breakpoint (cmd + option + B in Xcode) at “objc_exception_throw”.

Now run your app in the simulator under the debugger, and trigger memory warnings while working your app. Trigger often, at the places your app probably won’t expect them. In fact, try triggering as much as you can.
The shortcut you defined allows you to send the warnings while the mouse operates the app. Since NSZombie throws an exception on each message it receives, you’ll break exactly where you need.
Have fun and good luck!
Posted Sunday, March 14th, 2010 by Ofri Wolfus in Code, dPompa | No Comments »
Update: When writing this I wasn’t aware of CGRectIntegral() which is by far a better choice (as QC explains in the comments).
If you ever did extensive drawing with quartz, or any dynamic UI placement on the iPhone, you probably stumbled on this as well. I first saw it when drawing the oval shapes that show your priority in Matalot. The drawing code was very simple. Add an oval path, then fill + stroke. But to my surprise, when testing on the simulator the drawing was blurred. On my iPhone’s small screen it was less obvious, but blurred as well.
At first I thought something is wrong with my eyes. But then I added another instance of my view which rendered perfectly. There I was, with two instances of the same view, one blurred and one with the crisp anti-aliasing you’d expect from quartz. WTF?!
After many sleepless hours I suddenly remembered an article I once read about font rendering. In that article they discussed the rendering issues and how Windows draws fonts differently from OS X. Writing about this made me look up this article. It’s a very interesting reading, especially if graphics is your thing. Also be sure to read the linked articel about sub-pixel rendering.
Back to the subject, Apple and many others are using sub-pixel rendering to get beautiful anti-aliasing. Apple also prefers to stay loyal to the original drawing, even at cost of slight blurring. So perhaps my drawing just falls under this category? At this point I stepped back and checked out the exact placement and size of my views. And guess what, the blurred one was placed at non-integer pixels.
It turns out that quartz rendering gets blurred when placed at non-integer pixels! Rounding each pixel of the blurred view’s frame magically drew a beautiful result. How did I end up with non-integer values in the first place you ask? Simple. Placing a view at otherView.bounds.size.width / 2.0 may result in something like 45.5, and BOOM! You got blurred result.
Here is a quick function I use in order to make sure my views’ frames are with integer values. Feel free to use it as you wish.
static inline CGRect DPIntRect(CGRect rect) {
#if CGFLOAT_IS_DOUBLE
rect.origin.x = lrintl(rect.origin.x);
rect.origin.y = lrintl(rect.origin.y);
rect.size.width = lrintl(rect.size.width);
rect.size.height = lrintl(rect.size.height);
#else
rect.origin.x = lrintf(rect.origin.x);
rect.origin.y = lrintf(rect.origin.y);
rect.size.width = lrintf(rect.size.width);
rect.size.height = lrintf(rect.size.height);
#endif
return rect;
}
I’d love to hear if you also experienced this, especially if you have a different solution
Posted Tuesday, September 22nd, 2009 by Ofri Wolfus in Code | 2 Comments »