English 中文(简体)
某些类型和静态变量的外部变量之间的差异
原标题:Difference between external variable of some sort and static variable

某些类型和静态变量的外部变量之间的差别如何?

//ClassA.m
NSString *var1;
static NSString *var2;

@implementation ClassA

...
最佳回答

extern

<代码>extern 不变/可变,可在多个物体文档中查阅(或参考)。 它是一个全球出口C文号。 如果你想要从多种翻译单位(或汇编文件)获得经常/全球变量,或者如果你想在多个二读中加以使用(例如,你希望从你的手里加以使用,而且定义正在一个动态的图书馆)。 通常,你会宣布,其他人会使用。

static

<代码>static载有每种翻译的复印件。 汇编的每个档案(例如#included) 静态发送该静态文本。 你们应当避免这种情况。 这将产生一种模糊的双手,使执行变得非常混乱。 如果价值是当地档案,而且该档案应当是私人的,那么你应赞成静态。

由于这些原因,您应赞成static in You .c, m, .cpp, and .mm file, and extern< in Youheaders.

最后,NSString Pointer be const byail:

// declaration - file.h
extern NSString * const var1;

// definition - file.m
NSString * const var1 = @"var1";

static NSString * const var2 = @"var2";

If you still want m或e, here s a related answer I wrote, and here s another.

问题回答

<>全球变量和<代码>static

在任何方法之外(在目标-C中,或在目标-C中,在@implementation/code>/> @end 在全球变量的lifetime上,即在申请执行的整个期间将存在变量。

A global variable with no qualification, or only with const and/or volatile qualifiers, is visible from anywhere. So for example:

NSString *MyApplicationName;

宣布全球变量<代码>MyApplicationName,可在申请的任何地点查阅。

全球变数(但并非其寿命)的可见度可仅限于(更准确地说,“复合单位”允许包括他人在内的一个档案)中,将其附在<>static上。 例如:

static NSString *MyClassName;

宣布全球变量<代码>MyClassName,目前汇编单位仅可查。

Using static qualified global declarations within @implementation/@end is the closest thing Objective-C offers to other languages "class variables".

www.un.org/Depts/DGACM/index_french.htm 在标题档案中,如果没有<代码>static ,则会出现错误,如果存在不同可见度的多种不同变量,则会出现这种错误。

The extern qualifier

<代码>extern 等值相当不同。

最初的含义(现在允许稍微放松,见下文)只是界定了在其他地方宣布的全球变量的 类型和 姓名。 这就是你告诉汇编者,“是某种类型的全球变数,我喜欢使用,由另一个汇编单位提供”。 例如:

extern NSString *MyApplicationName;

states that some compilation unit contains the declaration (which must not be static qualified):

NSString *MyApplicationName;

<代码>extern 合格申报单本身并不造成全球变量的任何储存分配。

You do place extern qualified declarations in header files, this is how a compilation unit advertises the global variables it wishes to export. You can also place them in code files to reference globals variables declared elsewhere. Within a code file you can place an extern qualified declaration either outside of any method/function - in which case the name/type is available for to whole compilation unit - or within a method/function - in which case the visibility of the name/type is limited to just that method/function.

小型放松<>

最初,您必须有一个非<代码>extern ,方可满足任何<代码>extern/code>的要求。 合格者。 然而,如果将各个汇编单位连接在一起,则后来发生改变,没有一个汇编单位宣布一个全球变量,由一些<代码>extern/code>参考。 然后,附带条件的声明(通常吗?)只是产生变数本身,使变数增加,而不是产生“全球错”。 最好的方案拟订做法不是依靠这种放松——总是没有“<>t>extern,每个全球变量都有合格的声明。

I could give an explanation, but it wouldn t be as good as this one:

http://blog.ablepear.com/2010/01/objective-c-tuesdays-static-variables.html





相关问题
passing form variables into a url with php

I have the following form which allows a user to select dates/rooms for a hotel reservation. <form action="booking-form.php" method="post"> <fieldset> <div class="select-date">...

Error: "Cannot modify the return value" c#

I m using auto-implemented properties. I guess the fastest way to fix following is to declare my own backing variable? public Point Origin { get; set; } Origin.X = 10; // fails with CS1612 Error ...

C-style Variable initialization in PHP

Is there such a thing as local, private, static and public variables in PHP? If so, can you give samples of each and how their scope is demonstrated inside and outside the class and inside functions?

C#/.NET app doesn t recognize Environment Var Change (PATH)

In my C# app, I am programmatically installing an Oracle client if one is not present, which requires adding a dir to the PATH system environment variable. This all works fine, but it doesn t take ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to dynamically generate variables in Action Script 2.0

I have a for loop in action script which I m trying to use to dynamically create variable. Example for( i = 0 ; i &lt 3 ; i++) { var MyVar+i = i; } after this for loop runs, i would like to ...

热门标签