The example code "Handling a Complex Mul开发者_运维问答ti-Touch Sequence" in the Event Handling section of the iPhone Application Programming Guide provides an incomplete example that assumes the reader knows enough fill-in the blanks. I know enough to know that I don't know enough to do that without some clarification.
In Listing 3-6, I assume touchBeginPoints
is a member property of type CFDictionaryRef
. Correct?
In that same example, we're using malloc()
so I assume we need to call free()
at some later point. My question is what am I freeing and when? Should I free()
the individual points in touchesEnded:
/touchesCancelled:
? How would I do that? (I assume I need to read up on enumerating CFDictionaryRef
) Or would I free(touchBeginPoints);
in my dealloc:
method?
Finally, in Listing 3-7 there is a compareAddress:
method. How (and where) would I implement that?
Update Found the answer to the last one.
- Yes, touchBeginPoints is a member property of type CFDictionaryRef.
- You wouldn't call free on an instance of CFDictionaryRef, but rather CFRelease. (CoreFoundation's memory management is similar to Objective-C's with explicit reference counting via retain/release semantics.)
- You may want to read "Collection Programming Topics for Core Foundation" to get a stronger feel for CFMutableDictionary. http://developer.apple.com/mac/library/documentation/CoreFoundation/Conceptual/CFCollections/CFCollections.html
- As for
free
ing the keys, you probably want to pass in a custom release callback when you create the dictionary. The key release callback is called for each element in the dictionary when the dictionary itself is released. (See the last two parameters toCFMutableDictionaryCreate
for more.)
compareAddress:
would look something like:
@interface UITouch (TouchSorting)
- (NSComparisonResult)compareAddress:(id)obj;
@end
@implementation UITouch (TouchSorting)
- (NSComparisonResult)compareAddress:(id)obj {
if ((void *)self < (void *)obj) return NSOrderedAscending;
else if ((void *)self == (void *)obj) return NSOrderedSame;
else return NSOrderedDescending;
}
@end
精彩评论