0

I want to pass some data to the fire method.So I use the 'userInfo'

I did like this:

struct MyStruct* userinfo = malloc(sizeof(struct MyStruct));

userinfo->a = 1;
userinfo->b = 2;

NSTimer *myTimer = [NSTimer scheduledTimerWithInterval:0.05 target:self selector:@selector(myFireMethod:) userInfo:userinfo repeats:YES];

But the problem happened,the iOS app crashed when running the scheduledTimerWithInterval method.

It must be something wrong with 'userinfo' .What's the probably mistake?

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Rafer
  • 1,170
  • 2
  • 10
  • 15

1 Answers1

1

userInfo has to be Objective C object, cause it's retained during assignment.

If you want to pass C-struct you have to wrap it with NSValue:

NSValue* val = [NSValue valueWithPointer: your_struct_ptr];
Max
  • 16,679
  • 4
  • 44
  • 57