我可以在Objective-C中内联静态类方法吗? [英] Can I inline static class methods in Objective-C?

查看:73
本文介绍了我可以在Objective-C中内联静态类方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以将函数声明为内联,如下所示:

You can declare functions as inlines like this:

#ifdef DEBUG
void DPrintf(NSString *fmt,...);
#else
inline void DPrintf(NSString *fmt,...) {}
#endif

因此,当您不在调试状态时,该函数不会产生任何成本,因为它已经过优化和内联.如果您想拥有相同的东西但要使用类方法该怎么办?

so that when you're not in DEBUG, there's no cost to the function because it's optimized and inline. What if you want to have the same thing but for a class method?

我的班级是这样宣告的:

My class is declared like this:

@interface MyClass : NSObject {

}

    + (void)DPrintf:(NSString *)format, ...;
    // Other methods of this class
@end

我想将'DPrintf'转换为与inline类似的东西,以便调用该方法没有成本.

I want to convert 'DPrintf' into something similar to the inline so that there's no cost to invoking the method.

但是我不能这样做:

inline +(void)DPrintf:(NSString *)format, ...; {}

如何为非调试编译关闭类的零成本静态方法?

How can I have a zero-cost static method of a class turned off for non-debug compilations?

推荐答案

请小心. Objective-C方法与C函数不同.编译器将Objective-C方法转换为objc_msgSend()函数调用;您无法控制方法是否是内联的,因为这是无关紧要的.您可以阅读有关Objective-C运行时的更多信息

Be careful. Objective-C methods are not the same as C functions. An Objective-C method is translated by the compiler into the objc_msgSend() function call; you don't have control over whether a method is inline or not because that is irrelevant. You can read more about the Objective-C runtime here (Objective-C Runtime Programming Guide), here (Objective-C Runtime Reference), and here (CocoaSamurai post), and a quick Google search should bring up more info.

这篇关于我可以在Objective-C中内联静态类方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆