简体   繁体   English

NSTimer iOS4中的userinfo

[英]userinfo in NSTimer iOS4

I want to pass some data to the fire method.So I use the 'userInfo' 我想将一些数据传递给fire方法。所以我使用了'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. 但是问题发生了,运行scheduledTimerWithInterval方法时,iOS应用程序崩溃了。

It must be something wrong with 'userinfo' .What's the probably mistake? 'userinfo'一定有问题。可能是什么错误?

userInfo has to be Objective C object, cause it's retained during assignment. userInfo必须是Objective C对象,因为在分配过程中会保留它。

If you want to pass C-struct you have to wrap it with NSValue: 如果要传递C结构,则必须用NSValue包装它:

NSValue* val = [NSValue valueWithPointer: your_struct_ptr];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM