ios - 如何声明静态变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16955379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ios - How to declare static variable?
提问by user2439531
Static Variable declared in C# like this :
在 C# 中声明的静态变量如下:
private const string Host = "http://80dfgf7c22634nbbfb82339d46.cloudapp.net/";
private const string ServiceEndPoint = "DownloadService.svc/";
private const string ServiceBaseUrl = Host + ServiceEndPoint;
public static readonly string RegisteredCourse = ServiceBaseUrl + "RegisteredCourses";
public static readonly string AvailableCourses = ServiceBaseUrl + "Courses";
public static readonly string Register = ServiceBaseUrl + "Register?course={0}";
How to call this static variable in another class ?
如何在另一个类中调用这个静态变量?
回答by Bhavin
Answer :use the static
keyword.
答:使用static
关键字。
Syntax :static ClassName *const variableName = nil;
( Updated [ Added const
]as per the Comment by Abizern)
语法:(根据Abilern的评论static ClassName *const variableName = nil;
更新[添加const
])
Reason for update ( As per the comments by "Till " ):
static
when being used on a variable within a function/method will preserve its state even when the scope of that variable is left. When being used outside any function/method, it will make that variable invisible to other source files - it will be visible within that implementation file only when being used outside any function/method.
Hence a const
with static
helps the compiler to optimize it accordingly.
更新原因(根据“Till”的评论):
static
当在函数/方法中的变量上使用时,即使该变量的作用域已离开,也将保留其状态。当在任何函数/方法之外使用时,它将使该变量对其他源文件不可见 - 只有在任何函数/方法之外使用时,它才会在该实现文件中可见。因此const
withstatic
帮助编译器相应地优化它。
If you need more explanation on use of const
with static
, I found one beautiful link here : const static.
如果您需要更多有关const
with 的使用说明static
,我在这里找到了一个漂亮的链接:const static。
Use:
用:
You might have seen the use of "static"keyword in your tableview's delegate - cellForRowAtIndexPath:
您可能已经在 tableview 的委托中看到了“static”关键字的使用- cellForRowAtIndexPath:
static NSString *CellIdentifier = @"reuseStaticIdentifier";
static NSString *CellIdentifier = @"reuseStaticIdentifier";
回答by MaxGabriel
static NSString *aString = @""; // Editable from within .m file
NSString * const kAddressKey = @"address"; // Constant, visible within .m file
// in header file
extern NSString * const kAddressKey; // Public constant. Use this for e.g. dictionary keys.
To my knowledge public statics aren't a built-in feature of Objective-C. You can work around this by making a public class method that returns the static variable:
据我所知,公共静态不是 Objective-C 的内置功能。您可以通过创建一个返回静态变量的公共类方法来解决此问题:
//.h
+ (NSString *)stringVariable;
//.m
static NSString * aString;
+ (NSString *)stringVariable
{
return aString;
}
Most static objects can't be initialized at compile time (I think only strings actually). If you need to initialize them, you can do so in the + (void)initialize
method, which lazily gets called whenever that class is first referenced.
大多数静态对象不能在编译时初始化(我认为实际上只有字符串)。如果您需要初始化它们,您可以在+ (void)initialize
方法中进行,只要第一次引用该类,就会延迟调用该方法。
static UIFont *globalFont;
+ (void)initialize
{
// Prevent duplicate initialize http://www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html
if (self == [ClassName class]) {
globalFont = [UIFont systemFontOfSize:12];
}
}
回答by Amitg2k12
Objective C is super set of C/C++ , so for static it follows C++/C convention, you should be able to use it
Objective C 是 C/C++ 的超集,因此对于静态它遵循 C++/C 约定,您应该可以使用它
static <<datatype>> <<variableName>> = initialization
Hoping you would have tried this way , have you got any error, if yes, please add more clarity in your question
希望您尝试过这种方式,您是否有任何错误,如果是,请在您的问题中更加明确
if thats case with NSString
use following,
如果NSString
使用以下的情况,
static NSString *pString = @"InitialValue";
and if you have to modify NSString
in your code, make sure it must be NSMutableString
.
如果您必须修改NSString
代码,请确保它必须是NSMutableString
.
Hope that's help ...
希望这有帮助...