1

I want to minimize the amount of Obj-C code in my project.

As part of this effort it becomes necessary to abstract the multitouch UITouch data, to instantiate some C++ classes that will manage hold the data as I work on it from C++, rather than working with it directly via objective C. There are many reasons for doing it this way (such as being able to hook into the code using any arbitrary form of device input).

Here's the API I've got so far. I have an InputHandler class which has these public methods:

// c++
void beginPoint(float x, float y, void* touchUID);
void endPoint(float x, float y, void* touchUID);
void movePoint(float x, float y, void* touchUID);
void cancelPoint(float x, float y, void* touchUID);

which correspond to - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event and so on within my ViewController.mm.

I need to track the individual fingers separately, and the documentation makes it pretty clear that each finger gets assigned its own UITouch instance (i.e. the UITouch object actually represents a specific finger). This would indicate that I can get away with using the pointer value as a Unique ID for a finger-tracking point.

However, I run into trouble here: enter image description here

I need to learn about bridged casts now. I'll be honest, I don't know a thing about how ARC works because I barely understand retain/release yet. I am fully invested in shared_ptr and friends from C++ to help me manage complex systems.

I clearly need to just leave the UITouch* touch alone, and let it be retained/release/ARC'd exactly as if I did not access it. But even for something as basic as this, it is not clear which of the three bridged cast options I need to use.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • Have a read here: http://stackoverflow.com/questions/7036350/arc-and-bridged-cast – North-Pole Aug 25 '13 at 20:51
  • Yes, thanks, I've already seen that post. However too many keywords (in particular `retainable`) mean nothing to me. Also, what is a `CFType`? CoreFoundation type? That doesn't apply here, I wrote my C++ struct. – Steven Lu Aug 25 '13 at 20:52
  • All that being said, @monkeydom's line "If you have no inclination to use the conversions above, use this one." is reassuring and I will use a `__bridge` cast. – Steven Lu Aug 25 '13 at 20:55
  • there are memory management rules to keep in mind here: if you bridge cast without transferring ownership (a retain), the object will be deallocated behind your back. if you bridge cast and transfer ownership, you are responsible for releasing the object when you are done with it. – Stefan Fisk Aug 25 '13 at 21:25

1 Answers1

0

If youre ok with the UITouch object going away after the method returns, a simple __bridge cast is all you need. Youre fine to use the memory address of the UITouch object as a key as long as you dont ever dereference it. Itll be dangling soon after the method returns.

atomkirk
  • 3,701
  • 27
  • 30