Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass objective c object and primitive type into a void *

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.

like image 920
user674669 Avatar asked Dec 05 '25 20:12

user674669


1 Answers

You can do this:

struct MyStruct {
    __unsafe_unretained UIImage *img;
    int i;
}

However, if you do this, you must keep in mind that the img field will not automatically retain and release its UIImage.

If you post more details about your “method that only takes a (void *)”, we might be able to give you better advice.

EDIT

You have updated your question to say that you need to pass a void * to the animationDidStop:finished:context: selector. Since you're using ARC, I know you're targetting iOS 4.0 or later, so you should just use animateWithDuration:animations:completion: instead. Then you don't need to pass a void *.

UIImage *img = someImage();
int i = someInt();

[UIView animateWithDuration:2.0 animations:^{
    // set the animatable properties here
} completion:^(BOOL finished) {
    doSomethingWithImageAndInteger(img, i);
}];
like image 164
rob mayoff Avatar answered Dec 08 '25 11:12

rob mayoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!