I want to pass 2 variables:
UIImage * img
int i
into another method that only takes a (void *)
I tried making a C struct containing both img and i
struct MyStruct {
UIImage *img;
int i;
}
but xcode gives me an error saying “ARC forbids Objective-C objects in structs or unions”
The next thing I tried is to write an objective-c class MyStruct2 containing img and i, alloc-initing an instance of it and typecasting it as (__bridge void*) before passing it to the method. Seems little involved for my use case. Seems like there should be a better way.
What’s the simplest way to achieve this?
Thank you.
Edit based on comments: I have to use void * as it is required by the UIView API. I created a selector as mentioned by UIVIew API
+ (void)setAnimationDidStopSelector:(SEL)selector
Please see documentation for setAnimationDidStopSelector at http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html . It says … The selector should be of the form:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
I want to pass both img and i into the (void *)context argument.
ARC cannot follow ObjC objects in plain old C structs. It tracks pointers on the stack, or in ObjC and C++ objects, which are copied using methods, explicit or implicit, but under the compiler control anyway.
So… use a class. C++ if you like it, or :
and there you go.
The solution needs more lines, that’s true, but you have a much better control on how things are done, including the image life cycle. You can use a strong pointer, or a weak one if it suits your needs better.
Of course, you can also use either a dictionary or an array. Literals make that especially easy:
Pretty easy.
edit
Forgot the transfer part, oops :). So then:
You must use the following annotations:
arguments is retained, and that reference is no longer managed by ARC.
When you receiver your pointer back, you must convert it:
Using __bridge only may result in a dangling pointer if the last known reference disappear. If you want that void* to retain the dictionary, you must use __bridge_retained first, then __bridge_transfer.