C中的严格类型检查第2部分

C中的严格类型检查第2部分

本文介绍了Objective-C中的严格类型检查第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在中,我一直在寻找一种方法,使用define来确保变量属于某种类型。但有时我会遇到这种情况:

In this question I was looking for a way to ensure that a variable is of a certain type using a define. But sometimes I have this situation:

- (void) theSituation:(Thinger*)thinger {
    ASSERT_IS_KIND_OF(thinger, Thinger);
    // etc. etc.

然后我开始考虑,进行调试仅限目的,能够调用以下内容可能会很好:

Then I started thinking that, for debugging purposes ONLY, it might be nice to be able to call something like:

- (void) theSituation:(Thinger*)thinger {
    ASSERT_INPUT_PARAMS_ARE_OF_CORRECT_TYPES();
    // etc. etc.

问题是:你能检查一下a的参数吗?方法是正确的类型(使用 isKindOf )通过 #define ,你会怎么做(一般来说) )?

The question is: could you check that parameters of a method are of the right type (using isKindOf) via #define, and how would you do this (in general terms)?

推荐答案

获取方法的参数时(使用 method_copyArgumentType 如另一个答案中所提到的,它返回的类型是C类型(如int,float等)或只是Object(返回为@)。遗憾的是,不可能得到方法所期望的Objective-C类型 - 编译时信息会丢失。

When you get the arguments of a method (using method_copyArgumentType as mentioned in the other answer), the "type" it returns is either a C type (like int, float, etc) or just Object (returned as a "@"). Sadly it's not possible to get the objective-C type that a method is expecting — that information is lost when you compile.

找到类似问题的答案。

这篇关于Objective-C中的严格类型检查第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:15