在目标C中使用extern [英] Use of extern in Objective C

查看:71
本文介绍了在目标C中使用extern的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在目标C中使用extern有多好? 确实使某些部分的编码变得容易..但这不会破坏对象的方向吗?

How good is it to use extern in Objective C? It does make coding for some parts easy.. but doesn't it spoil the object orientation?

推荐答案

您会发现extern在Cocoa框架中得到了广泛使用,并且很难找到令人信服的论点,即他们的OO是被宠坏了".相反,可可被很好地封装,并且通常通过外部暴露其必须的东西.全局定义的常量无疑是最常见的用法,但不一定是唯一的有效用法.

You'll find that extern is used extensively in the Cocoa frameworks, and one would be hard-pressed to find a convincing argument that their OO is "spoiled". On the contrary, Cocoa is well-encapsulated and only exposes what it must, often via extern. Globally-defined constants are certainly the most common usage, but not necessarily the only valid use.

IMO,使用extern不一定会破坏"面向对象.即使在OO中,也经常使用可从任何地方访问的变量.使用extern是在Objective-C中缺少类变量"(如Java中用static声明的变量)的最常见解决方法.它允许您扩展符号的引用范围,超出了声明符号的编译单元的范围,本质上是通过保证符号会在某人定义的位置进行引用.

IMO, using extern doesn't necessarily "spoil" object orientation. Even in OO, it is frequent to use variables that are accessible from anywhere. Using extern is the most frequent workaround for the lack of "class variables" (like those declared with static in Java) in Objective-C. It allows you to expand the scope in which you can reference a symbol beyond the compilation unit where it is declared, essentially by promising that it will be defined somewhere by someone.

您也可以将extern__attribute__((visibility("hidden")))组合在一起,以创建可以在其编译单元外部使用但不能在其链接单元外部使用的符号.我已将其用于自定义库和框架代码,以正确封装更高级别的内部细节.

You can also combine extern with __attribute__((visibility("hidden"))) to create a symbol that can be used outside its compilation unit, but not outside its linkage unit, so to speak. I've used this for custom library and framework code to properly encapsulate higher-level internal details.

这篇关于在目标C中使用extern的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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