24
  1. メソッドまたは変数宣言の前にこの単語を使用する場合、externそれをグローバルにして、プロジェクト全体で読み取り/書き込み/使用可能にしますか?

  2. キーワードの前にexternを使用した場合、プロジェクトの一部でまだアクセスできない可能性はありますか?たとえば、「保護された」を使用する場合など、サブクラスによってのみ。

  3. externCキーワードですよね?Objective-Cに同等のものはありますか?彼らがObjective-CプロジェクトでCキーワードを使用する理由を実際には理解していません。

ありがとう

4

3 に答える 3

39

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

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) 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.

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;

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

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) 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.

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;
于 2011-09-07T07:20:34.390 に答える
17

extern doesn't mean "global", it means "defined elsewhere". It is used to tell the compiler that a variable or function exists (in another object file or library), so that it shall not complain about it and that the linker will be provided with that object file or library.

As a consequence extern implies that the target item is global.

Objective-C is just a superset of C. Everything that is available in C is available in Objective-C as well, with the same syntax and semantic. There is no construct of C that is defined in another way in Objective-C.

于 2011-09-07T07:21:34.917 に答える
0

Point 3: Yes you can use FOUNDATION_EXPORT in objective C which is a macro that resolves to different keyword depending if compiling C or C++

More info here on the differences: "FOUNDATION_EXPORT" vs "extern"

于 2016-01-06T05:42:32.453 に答える