grand-central-dispatch - dispatch_block_create - “调用‘dispatch_block_create’没有匹配的函数”
问题描述
我正在尝试在.mm
文件中使用 dispatch_block_create 创建一个块
dispatch_block_t testBlock = dispatch_block_create(0, ^{
NSLog(@"Hello");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue(), testBlock);
dispatch_block_cancel(testBlock);
Xcode 抱怨:
调用“dispatch_block_create”没有匹配的函数
我什至尝试添加:
#import <dispatch/block.h>
解决方案
您遗漏了编译器错误的关键部分。我得到:
foo.mm:6:34: error: no matching function for call to 'dispatch_block_create'
dispatch_block_t testBlock = dispatch_block_create(0, ^{
^~~~~~~~~~~~~~~~~~~~~
/usr/include/dispatch/block.h:171:1: note: candidate function not viable: no known conversion from 'int' to 'dispatch_block_flags_t' for 1st argument
dispatch_block_create(dispatch_block_flags_t flags, dispatch_block_t block);
^
1 error generated.
关键部分是“候选函数不可行:第一个参数没有从 'int' 到 'dispatch_block_flags_t' 的已知转换”。这是由于 (Objective-)C++ 的类型检查更严格。您需要显式0
转换为dispatch_block_flags_t
:
dispatch_block_t testBlock = dispatch_block_create(static_cast<dispatch_block_flags_t>(0), ^{ ... });
推荐阅读
- saxon - Saxonica - .NET API - XQuery - XPDY0002:轴步根/后代::xxx 的上下文项不存在
- java - 将 java 编译为与 C/C++ 兼容的目标文件
- laravel - Laravel 的时间戳有奇怪的返回格式
- python - 根据 ids 和 category 创建 groupby 列
- git - git filter 分支到 git filter repo 转换
- c++ - 在c ++中将数组从函数返回到main
- r - 在grid.arrange上使用rot =旋转标签不起作用
- python - sqlite3中的主键
- java - 使用 Criteria Builder API 在 JPA 中加入自定义查询
- scala - Scala - 回调的返回值