gpt4 book ai didi

ios - block 可以捕获 CoreFundation 对象吗?

转载 作者:可可西里 更新时间:2023-11-01 05:44:10 26 4
gpt4 key购买 nike

Apple's doc当我想捕获一个 CoreFoundation 对象时,我找不到我可以做什么。

但在 Apple 的 Concurrency Programming Guide .当调度对象不支持 ARC 时,示例代码似乎使用了一些代码,就像这样:

   void average_async(int *data, size_t len, dispatch_queue_t queue, void (^block)(int))

{

// Retain the queue provided by the user to make

// sure it does not disappear before the completion

// block can be called.

dispatch_retain(queue);


// Do the work on the default concurrent queue and then

// call the user-provided block with the results.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

int avg = average(data, len);

dispatch_async(queue, ^{ block(avg);});



// Release the user-provided queue when done

dispatch_release(queue);

});
}

我是否需要像之前的DispatchObject那样使用CFObject。但是如果我需要多次调用该 block 呢?

也许我可以使用__attribute__((NSObject)),但我不知道会发生什么!

Apple 对此有说明吗?

最佳答案

我没有在 Apple 明确看到任何东西,但我确实看到了一些 mentions in the llvm.org documentation ,我在 in this cocoa-dev mailing list thread 上找到了详细说明.

看起来你应该可以使用 __attribute__((NSObject)),因为它被赋予了隐式的“__strong”资格(来自文档)并且在实际上(来自邮件列表线程),当 block 排队时对象被保留并在 block 完成时被释放。

关于ios - block 可以捕获 CoreFundation 对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26090935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com