Tracking Down Drawing Errors

Do you sometimes see this in your code while performing custom drawing using drawRect?

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

Storyboard Localization

Internationalizing the users interface in Xcode is really easy. Xcode separates all the texts from your views in a dictionary. However, we can make it better, using extensions and @IBDesignables. How handy would it be, if setting localized strings were as easy as the following?

Detailed Exceptions

There are a lot of tools in Xcode that help us during the debugging process. No doubt. Breakpoints for example are these tiny signals that tell the debugger to temporarily suspend execution of program at a certain point. My favourite type of breakpoints are the exceptional ones. Exceptional breakpoint is this guardian that pauses the execution of our program, as soon as it knows that things are going to be pretty bad. When this happens, we are usually presented with all the stack trace and in the blink of an eye we know what happened that was bad. This is the usual scenario.

But sometimes we see things like that:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:1623

Which with tells us literally nothing, when we are not familiar enough with the code that we are working. We get sterile error message that tells us that we are doing something wrong with UITableView animations, but it’s all we get out of the box.

However, there is a way to get more detailed info. The thing that we can do to know the issue of our crash is this:

  1. Go do debug navigator
  2. Select objc_exception_throw frame
  3. Go to console
  4. Type in: po $eax when using simulator or po $r0 when debugging on a device.

This way you get more detailed error description which tells you a lot more about the place that you should be looking for to find your mistake.

Toggle Slow Animations

iOS Simulator has a feature that slows animations, you can toggle it either by pressing ⌘T or choosing Debug > Toggle Slow Animations in Frontmost App. It’s very useful, but what if we want to do the same on device? It’s easy, fast and simple.

CALayer has a property called speed, which is a time multiplier. This means that if we have an animation with a duration of 1 second, and set the layer’s speed to 2, it’ll take just 0.5 seconds to finish. The best thing about it is that it’s related to the parent layer. So when we change the speed of a particular CALayer, every child layer will be affected. So, if we change UIWindow layer speed, every CALayer in our application will perform animations with that custom speed value. That leaves us with this two extensions: