【问题标题】:ios - How to declare static variable? [duplicate]ios - 如何声明静态变量? [复制]
【发布时间】:2013-06-02 01:34:01
【问题描述】:

像这样在 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}";

如何在另一个类中调用这个静态变量?

【问题讨论】:

  • 由于“静态变量”在 Objective-C 中不是一个定义明确的术语,并且不同的含义经常被混淆,所以不清楚你在这里问什么。你想要一个可以通过类接口访问的类变量吗?还是与静态存储持续时间有关?
  • 在这里我认为“静态变量”在 Objective-C 中定义得非常好,与 C 中的含义完全相同。

标签: iphone ios objective-c variables static


【解决方案1】:

答案:使用static 关键字。

语法: static ClassName *const variableName = nil;(根据Abizern的评论更新[添加const]


更新原因(根据“Till”的cmets)static 在函数/方法内的变量上使用时,即使离开该变量的范围,也会保留其状态。当在任何函数/方法之外使用时,它将使该变量对其他源文件不可见 - 只有在任何函数/方法之外使用时,它才会在该实现文件中可见。 因此,conststatic 有助于编译器相应地对其进行优化。

如果您需要更多关于 conststatic 的使用说明,我在这里找到了一个漂亮的链接:const static


用途:

您可能已经在 tableview 的委托 - cellForRowAtIndexPath:

中看到了 "static" 关键字的使用

static NSString *CellIdentifier = @"reuseStaticIdentifier";

【讨论】:

  • 我会写static NSString * const staticURL = @"something";
  • @Abizern:你能解释一下const背后的原因吗?
  • @Vin 该变量在运行时永远不会被更改,因此 const 有助于编译器相应地对其进行优化。
  • @Till:如果是这样,那么“static 在该行中的作用是什么?”或“conststatic 是使用static 的更佳方式”。
  • @Vin static 在函数/方法内的变量上使用时,即使离开该变量的范围,也会保留其状态。当在任何函数/方法之外使用时,它将使该变量对其他源文件不可见 - 只有在任何函数/方法之外使用时,它才会在该实现文件中可见。
【解决方案2】:
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.

据我所知,公共静态不是 Objective-C 的内置特性。您可以通过创建一个返回静态变量的公共类方法来解决此问题:

//.h
+ (NSString *)stringVariable;

//.m
static NSString * aString;
+ (NSString *)stringVariable
{
    return aString;
}

大多数静态对象不能在编译时初始化(我认为实际上只有字符串)。如果您需要初始化它们,您可以在 + (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];
    }
}

【讨论】:

    【解决方案3】:

    Objective C 是 C/C++ 的超集,所以对于 static 它遵循 C++/C 约定,你应该可以使用它

    static <<datatype>> <<variableName>> = initialization
    

    希望您会尝试这种方式,您是否有任何错误,如果有,请在您的问题中添加更多清晰度

    如果 NSString 是这种情况,请使用以下,

    static NSString *pString = @"InitialValue";
    

    如果您必须在代码中修改NSString,请确保它必须是NSMutableString

    希望对你有帮助……

    【讨论】:

    • 如果它是静态的,为什么要修改它?
    • 您必须对此进行更多解释:修改字符串。因为这也是有效的:myString = [NSString stringWithFormat:@"Add string %@", myString];
    • Objective-C 是 C 的超集,不涉及 C++。实际上,static 的语义在 C 和 C++ 之间略有不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 2011-11-10
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    相关资源
    最近更新 更多