Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access constants in Class without making instance of class in ObjectiveC [duplicate]

Below C# class is used just to keep constants. So we can access each using class name So 'Constants.DIR_ARCHIVE' will give 'Archive'.

How I can define below C# class in ObjectiveC?

 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";
}
like image 694
Afsal Meerankutty Avatar asked Mar 24 '23 04:03

Afsal Meerankutty


2 Answers

How I can define below C# class in ObjectiveC?

Constants aren't organized using classes in Objective-C. If they're to stay constants, the usual approach in Objective-C would be to choose a meaningful prefix and apply it to each of those names:

NSString* const AM_DIR_ARCHIVE = "Archive";
NSString* const AM_DIR_DEPARTMENTS = "Departments";
NSString* const AM_DIR_FORMS = "Forms";

You'd do that in in an implementation (.m) file, and then declare the names as externally defined constants in a corresponding header (.h) file:

extern NSString* const AM_DIR_ARCHIVE;
extern NSString* const AM_DIR_DEPARTMENTS;
extern NSString* const AM_DIR_FORMS;

Another approach, and the one used in the Foundation framework for finding standard system directories, is to use an enumeration whose value can be passed to one of several functions or methods:

enum {
   NSApplicationDirectory = 1,
   NSDemoApplicationDirectory,
   NSDeveloperApplicationDirectory,
   NSAdminApplicationDirectory,
   NSLibraryDirectory,
   NSDeveloperDirectory,
   NSUserDirectory,
   NSDocumentationDirectory,
   NSDocumentDirectory,
   //...
};
typedef NSUInteger NSSearchPathDirectory;

You pass any of these values into a function like NSSearchPathForDirectoriesInDomains(). For that particular function, you get back an array of paths that match the specified directory.

like image 95
Caleb Avatar answered Apr 07 '23 00:04

Caleb


Generally, you will want to use proper constants as Caleb has explained (+1).

However, it can (at times) be useful to wrap those constants in class methods:

@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

In some cases, it will be smart associate a class with its constants via its interface.

This can also be useful if strings are built (or not) in different ways, or there is additional logic to perform (e.g. enabled features or composition of URLs) -- depending on the class you are dealing with.

Passing around an object and selector can be easier when dealing with some interfaces, compared to functions or arbitrary instances (constant instances) which are composed using different means.

A well chosen prefix on the C symbol is ideal for most uses.

So expanding on this, you could declare:

@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

then write:

NSString * resourceDirectory = [MONResourceDirectoryName Forms];

You can also use C structs for grouping data. I'll demonstrate using CFStrings (which are NSStrings), in case you need an option which is ARC-compatible:

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")
};
like image 33
justin Avatar answered Apr 07 '23 00:04

justin