【问题标题】:How to pass pointer-to-pointer to NSTimer scheduledTimerWithTimeInterval with ARC如何使用 ARC 将指针传递给 NSTimer scheduledTimerWithTimeInterval
【发布时间】:2012-06-30 15:04:09
【问题描述】:

我正在尝试将间接指针传递给 NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 如下:

-(void)assignResultAfterDelay:(Result **)resultPtr {
    [NSTimer scheduledTimerWithTimeInterval:1000 
                                    target:self
                                  selector:@(assignResult:)
                                  userInfo:resultPtr  // how to do this?
                                   repeats:NO];
 }

这不起作用,因为从 (Result * __strong *) 强制转换为 id,这会导致“间接指针隐式转换到 Objective-C 指向 'id' 的指针”错误。

诸如(__bridge id)resultPtr、使用objc_unretainedPointer(resultPtr) 或将指针类型更改为(Result * __weak *) 等桥接类型转换的变体和组合都没有帮助。这样做的正确方法是什么?

我想我可以创建一个包装类来保存我的间接指针并发送它的一个实例,但这看起来很难看。有没有更好的方法?

【问题讨论】:

    标签: objective-c pointers automatic-ref-counting


    【解决方案1】:

    我想我明白了。 NSInvocation 让我编译:

    -(void)assignResultAfterDelay:(Result * __strong *)resultPtr {
        NSMethodSignature *sig = [self methodSignatureForSelector:@selector(assignResult:)];
        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
        [invocation setTarget:self];
        [invocation setSelector:@selector(assignResult:)];
        [invocation setArgument:resultPtr atIndex:0];
    
        [NSTimer scheduledTimerWithTimeInterval:1000 invocation:inv repeats:NO];
    }
    
    // where assign result looks like:
    -(void)assignResult:(Result * __strong *)resultPtr { ... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多