下面的C#类只用于保持常量。 所以我们可以使用类名访问每个 所以'Constants.DIR_ARCHIVE'会给'归档'。
如何在ObjectiveC中定义C#类以下?
public class Constants
{
//Resource Directory Names
public const string DIR_ARCHIVE = "Archive";
public const string DIR_DEPARTMENTS = "Departments";
public const string DIR_FORMS = "Forms";
public const string DIR_GOAL_TRACKING = "GoalTracking";
public const string DIR_ROLES = "Roles";
public const string DIR_HOMEWORK = "HomeWork";
public const string DIR_POSTINGS = "Postings";
public const string DIR_SIGNUP = "SignUp";
public const string DIR_SITE_CONFIG = "System";
public const string DIR_PORTFOLIO = "Portfolio";
public const string DIR_MEMBERDEFINITION = "Definitions";
//Integer Constants
public const int LOG_DEFAULT_DURATION = 1;
//Other Constants
public const string OP_STATUS_ERROR = "Error";
public const string OP_STATUS_SUCCESS = "Success";
public const string OP_STATUS_WARNING = "Warning";
}
答案 0 :(得分:4)
如何在ObjectiveC中定义C#类以下?
常量不是使用Objective-C中的类进行组织的。如果他们要保持常量,Objective-C中的常用方法是选择有意义的前缀并应用它给每个名字:
NSString* const AM_DIR_ARCHIVE = "Archive";
NSString* const AM_DIR_DEPARTMENTS = "Departments";
NSString* const AM_DIR_FORMS = "Forms";
您可以在实现(.m)文件中执行此操作,然后在相应的标头(.h)文件中将名称声明为外部定义的常量:
extern NSString* const AM_DIR_ARCHIVE;
extern NSString* const AM_DIR_DEPARTMENTS;
extern NSString* const AM_DIR_FORMS;
另一种方法,以及在Foundation框架中用于查找标准系统目录的方法,是使用an enumeration,其值可以传递给多个函数或方法之一:
enum {
NSApplicationDirectory = 1,
NSDemoApplicationDirectory,
NSDeveloperApplicationDirectory,
NSAdminApplicationDirectory,
NSLibraryDirectory,
NSDeveloperDirectory,
NSUserDirectory,
NSDocumentationDirectory,
NSDocumentDirectory,
//...
};
typedef NSUInteger NSSearchPathDirectory;
您将任何这些值传递给NSSearchPathForDirectoriesInDomains()
之类的函数。对于该特定函数,您将获得与指定目录匹配的路径数组。
答案 1 :(得分:1)
通常,你会想要使用适当的常量,正如Caleb所解释的那样(+1)。
但是,在类方法中包含这些常量可能(有时)很有用:
@interface MONColor : NSObject
+ (NSString *)colorName;
@end
@interface MONRedColor : MONColor
@end
MONRedColor.m
NSString * const MONColorName_Red = @"Red";
@implementation MONRedColor
+ (NSString *)colorName
{
return MONColorName_Red;
}
@end
在某些情况下,它会通过其界面将类与其常量相关联。
如果以不同方式构建(或不构建)字符串,或者还有其他逻辑要执行(例如,启用的功能或URL的组合),这也很有用 - 取决于您正在处理的类。
与使用不同方法组合的函数或任意实例(常量实例)相比,在处理某些接口时,传递对象和选择器会更容易。
C符号上选择好的前缀非常适合大多数用途。
如此扩展,您可以声明:
@interface MONResourceDirectoryName : NSObject
+ (NSString *)Archive;
+ (NSString *)Departments;
+ (NSString *)Forms;
+ (NSString *)GoalTracking;
+ (NSString *)Roles;
+ (NSString *)HomeWork;
+ (NSString *)Postings;
+ (NSString *)SignUp;
+ (NSString *)System;
+ (NSString *)Portfolio;
+ (NSString *)Definitions;
@end
然后写:
NSString * resourceDirectory = [MONResourceDirectoryName Forms];
您还可以使用C结构对数据进行分组。我将演示使用CFString
s(NSString
s),以防您需要与ARC兼容的选项:
struct t_mon_resource_directory_name {
CFStringRef const Archive;
CFStringRef const Departments;
CFStringRef const Forms;
CFStringRef const GoalTracking;
CFStringRef const Roles;
CFStringRef const HomeWork;
CFStringRef const Postings;
CFStringRef const SignUp;
CFStringRef const System;
CFStringRef const Portfolio;
CFStringRef const Definitions;
};
extern const struct t_mon_resource_directory_name MONResourceDirectoryName;
const struct t_mon_resource_directory_name MONResourceDirectoryName = {
.Archive = CFSTR("Archive"),
.Departments = CFSTR("Departments"),
.Forms = CFSTR("Forms"),
.GoalTracking = CFSTR("GoalTracking"),
.Roles = CFSTR("Roles"),
.HomeWork = CFSTR("HomeWork"),
.Postings = CFSTR("Postings"),
.SignUp = CFSTR("SignUp"),
.System = CFSTR("System"),
.Portfolio = CFSTR("Portfolio"),
.Definitions = CFSTR("Definitions")
};