简体   繁体   English

通过 NSTimer 传递一个 Char 数组

[英]Passing a Char array through NSTimer

I am trying to send a char array through the selector method in NSTimer.我正在尝试通过 NSTimer 中的选择器方法发送一个字符数组。 My targetMethod will take the first 36 values of the array and perform its function every interval, incrementing by 36 every time until it reaches the end of the char array.我的 targetMethod 将获取数组的前 36 个值并在每个间隔执行其函数,每次递增 36,直到到达 char 数组的末尾。 However, it appears as though NSTimer only allows you send NSObjects with userInfo when using the following format:但是,当使用以下格式时,似乎 NSTimer 只允许您使用 userInfo 发送 NSObjects:

[NSTimer scheduledTimerWithTimeInterval:2.0
    target:self
    selector:@selector(targetMethod:)
    userInfo:nil
    repeats:NO]

I haven't found a way to use NSDictionary to do it either, and I would prefer to not have to perform a costly conversion to an NSArray.我也没有找到使用 NSDictionary 来做到这一点的方法,而且我宁愿不必对 NSArray 执行代价高昂的转换。

The char array that I am sending contains anywhere from 36-9500 values.我发送的字符数组包含 36-9500 个值。 Is it possible to use a char array with NSTimer?是否可以将字符数组与 NSTimer 一起使用? If not, is there a way I could use a global pointer to refer to an array which could then be accessed by the pointer within the targetMethod?如果没有,有没有办法可以使用全局指针来引用一个数组,然后可以通过 targetMethod 中的指针访问该数组?

If you don't want to make a conversion to NSObjects as you believe it's too costly for your application, you can always just make the char array globally/publicly available.如果您不想转换为 NSObjects,因为您认为它对您的应用程序来说太昂贵了,您总是可以让 char 数组全局/公开可用。

Just create a property or instance variable for it and access it that way from within the selector.只需为它创建一个属性或实例变量,然后从选择器中以这种方式访问​​它。

Otherwise you'll have to convert to an NSString or NSData and pass it that way.否则,您必须转换为 NSString 或 NSData 并以这种方式传递。 I can't think of a way to cleanly override the passing of data other than that.我想不出一种方法来干净地覆盖除此之外的数据传递。

The userInfo object of an NSTimer does not need to be an NSDictionary , so you could in fact pass your NSArray as the userInfo . NSTimer的 userInfo 对象不需要是NSDictionary ,因此您实际上可以将NSArray作为userInfo传递。 However, I believe the first parameter of the target method will have the NSTimer instance itself passed, which may not be what you want.但是,我相信目标方法的第一个参数会传递NSTimer实例本身,这可能不是您想要的。 You might have better luck with dispatch_async , where you can pass a block to be executed in 2 seconds and call the method directly with your NSArray .使用dispatch_async可能会更好,您可以在 2 秒内传递要执行的块并直接使用NSArray调用该方法。

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

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