Search code examples
iosuikituiresponder

Retaining UITouch: allowed or not?


Here's a quote from the documentation of UITouch:

A touch object persists throughout a multi-touch sequence. Never retain a touch object when handling an event. If you need to keep information about a touch from one touch phase to another, copy that information from the touch.

And then here's another one from Event Handling Guide for UIKit Apps:

Listing 9-1 shows the main implementation of the TouchableView class and its touch handling methods. Each method iterates through the touches and performs the needed actions. The touchViews dictionary uses the UITouch objects as keys to retrieve the subviews being manipulated onscreen.

class TouchableView: UIView {
    var touchViews = [UITouch:TouchSpotView]()
    ...
}

Clearly, they're violating their own rules by using UITouch instances as dictionary keys...

So what's the deal with this? Should one retain UITouch instances for complex gesture tracking or not?


Solution

  • I filed a bug report with Apple regarding the inconsistency between the sample code and the UITouch documentation. The result is that the paragraph "Never retain a touch object when handling an event" has been removed from the UITouch documentation. Since the sample code retains UITouch objects, we can therefore assume that it is allowed.