传递与签名不匹配的块参数

编程入门 行业动态 更新时间:2024-10-13 16:23:11
本文介绍了传递与签名不匹配的块参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用基于块的API,偶然发现了一个场景,在该场景中,我传入了一个块签名,该签名的签名与该方法所期望的typedef参数不匹配.令我惊讶的是,编译器似乎对此并不关心,并且该应用程序也没有崩溃.这是预期的行为吗?示例:

I'm working with a block-based API and stumbled across a scenario where I was passing in a block parameter that had a signature that didn't match the typedef'd parameter the method was expecting. To my surprise, the compiler didn't seem to care about this, and the app didn't crash. Is this expected behavior? Example:

typedef void(^MyBlock)(); typedef void(^MyBlockWithParam)(id param); - (void)doWork { MyBlockWithParam block1 = ^(id param) { NSLog(@"block1: %@", param); }; MyBlock block2 = ^{ NSLog(@"block2"); }; [self loadData:block1]; [self loadData:block2]; } - (void)loadData:(MyBlockWithParam)block { block(@"foo"); }

推荐答案

提供一个空参数规范,如

Providing an empty arguments specification as in

typedef void(^MyBlock)();

表示未指定"的参数.因此,这两种类型在编写时是兼容的.将第一个声明更改为

means "unspecified" arguments. So the two types are compatible as written. Changing the first declaration to

typedef void(^MyBlock)(void);

指定该块不带任何参数,并且会出现错误.

specifies that the block takes no arguments and you'll get an error.

K& RC指定空的参数列表表示未指定". C块规范说,对于块类型声明,这不正确(请参见. clang.llvm/docs/BlockLanguageSpec.html#block-variable-declarations )但是:GCC和Clang都将K& R行为实现为语言扩展

K&R C specifies that an empty argument list means "unspecified". The C blocks spec says this is not true for block type declarations (cf. clang.llvm/docs/BlockLanguageSpec.html#block-variable-declarations) but: both GCC and Clang implement the K&R behavior as a language extension.

更多推荐

传递与签名不匹配的块参数

本文发布于:2023-11-15 14:21:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不匹配   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!