代码之家  ›  专栏  ›  技术社区  ›  TechBee

如何在Objective C中生成ConstantList类

  •  0
  • TechBee  · 技术社区  · 15 年前

    如何在应用程序的目标C中创建一个ConstantList类,所有使用常量的类都可以访问该类。

    就像在Actionscript中一样:

    public class ConstantList
    {
       public static const EVENT_CHANGE:String = "event_change";
    }
    

    当做 兰詹

    2 回复  |  直到 15 年前
        1
  •  1
  •   Vladimir    15 年前

    可以使用全局常量,如下所示:

    //MyConstants.m    
    NSString * const EVENT_CHANGE = @"event_change";
    
    // MyConstants.h
    extern NSString* const EVENT_CHANGE;
    

    现在包括 MyConstants.h 实现文件的头,您可以在其中使用事件\更改常量字符串

        2
  •  0
  •   tonklon    15 年前

    我推荐弗拉基米尔的方法。

    @interface Constants : NSObject {
    }
    + (NSString*)aConstantString;
    @end
    
    @implementation Constants
    + (NSString*)aConstantString {
        return @"This is always the same and accessible from everywhere";
    }
    @end
    

    NSString* string = [Constants aConstantString];