约3的extern问题在一个Objective-C项目中使用 [英] 3 questions about extern used in an Objective-C project

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

问题描述


  1. 当我使用这个词的extern 的方法或变量声明之前,我会让它成为全局的,所以可读/写/可用在整个项目中?

  1. When I use the word extern before a method or variable declaration, am I making it global and therefore readable/writable/usable over the entire project ?

如果我在关键字前使用extern,有没有可能现在还没有通过我的项目的一部分访问?例如,仅供子类..比如,当我使用的保护。

If I use extern before a keyword, is there any chance it is still not accessible by part of my project ? For example, only by subclasses.. such as when I use "protected".

的extern 是一个C关键字,对不对?是否有在Objective-C的等效?其实我不明白为什么他们使用一个Objective-C项目一个C关键字。

extern is a C keyword, right? Is there an equivalent in Objective-C? I actually don't understand why they use a C keyword in an Objective-C project.

感谢

推荐答案

1)你指定的链接。 extern链接允许您或任何客户端引用符号。

1) you're specifying its linkage. extern linkage allows you or any client to reference the symbol.

对于全局变量:如果变量是可变的和/或需要适当建设,那么你应该考虑此对象的方法或函数。显着的例外情况是NSString的常量:

regarding global variables: if the variable is mutable and/or needs proper construction, then you should consider methods or functions for this object. the notable exception to this is NSString constants:

// MONClass.h
extern NSString* const MONClassDidCompleteRenderNotification;
// MONClass.m
NSString* const MONClassDidCompleteRenderNotification = @"MONClassDidCompleteRenderNotification";

2)还有就是extern关键字影响可见性(公共/保护/私营/包),任何情况下。使用符号(例如常数或C函数),只要有它在声明的标题。

2) there is no case where the extern keyword affects visibility (public/protected/private/package). to use the symbol (e.g. the constant or C function), simply include the header it is declared in.

如果你是新来的语言有些混乱:将外部C声明(常量,函数)之间 @interface ... @end 不会改变其范围:

somewhat confusing if you are new to the language: placing extern C declarations (constants, functions) in between @interface ... @end will not alter its scope:

@interface MONClass : NSObject

extern const size_t MaximumThreads;

@end

有相同的范围(全局)和可见性(公共)为:

has the same scope (global) and visibility (public) as:

@interface MONClass : NSObject

@end

extern const size_t MaximumThreads;

所以它真的是没有意义的放置类相关的C常量或函数在 @interface ... @结束 @implementation。 .. @结束。我建议在相同的标题作为接口,外将这些@接口/ @结束 @执行/ @结束和$ p $与它相关联,像这样的类pfixing名称:

so it really makes no sense to place your class related C constants or functions in the @interface...@end and @implementation...@end. i recommend placing these in the same header as the interface, outside @interface/@end and @implementation/@end and prefixing the name with the class it is associated with, like so:

@interface MONClass : NSObject

@end

extern const size_t MONClassMaximumThreads;
// MONClass.m
const size_t MONClassMaximumThreads = 23;

如果你想那个常数是私有的,只是声明和定义它是这样的:

and if you want that constant to be private, just declare and define it like this:

// MONClass.m
static const size_t MONClassMaximumThreads = 23;

@implementation MONClass

@end

不幸的是,没有同样简单或普通的方法,使该常数与objc保护

unfortunately, there is no equally simple or common way to make this constant protected with objc.

最后,您还可以使用类的方法,如果人数应有所不同类:

finally, you can also use class methods if the number should vary by class:

@interface MONMammal : NSObject
+ (NSUInteger)numberOfLegs;
@end

@implementation MONDog
+ (NSUInteger)numberOfLegs { return 4; }
@end
@implementation MONHuman
+ (NSUInteger)numberOfLegs { return 2; }
@end

3)是的,在其他语言中。例如,如果你使用的是C ++编译的extern const int的东西,C ++的翻译会寻找的东西声明一个extern C ++的象征。有在objc没有取代; objc是C的超集,并继承了所有的C的功能的。使用的extern的结构良好,你也可以找到它的框架使用(如基金会)。他们使用它,因为他们需要指定联动。 objc不提供替代品,presumably,因为它并不需要更换或扩展。

3) yes, among other languages. for example, if you use extern const int Something in a c++ translation, the c++ translation will look for Something declared as an extern C++ symbol. there is no substitution in objc; objc is a superset of C and inherits all of C's functionalities. use of extern is well formed and you can also find it in the frameworks you use (e.g. Foundation). they use it because they need to specify linkage. objc does not offer a substitute, presumably because it did not require a replacement or extension.

要避免这种情况,只需使用的#define 是这样的:

to avoid this, simply use a #define like this:

#if !defined(__cplusplus)
#define MONExternC extern
#else
#define MONExternC extern "C"
#endif

MONExternC const size_t MONClassMaximumThreads;

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

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