5

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";
}
Rui Peres
  • 25,741
  • 9
  • 87
  • 137

2 Answers2

5

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Can you please give me a sample code. I want to define all constants in a class like above and use it globally in ObjectiveC. – Afsal Meerankutty Apr 25 '13 at 06:39
  • @AfsalMeerakutty Again, constants aren't defined in classes in Objective-C. I've explained how Foundation tackles the task that you seem to be describing, so perhaps that will help you. But, in general, you shouldn't expect every C# practice to work the same way in other languages. If there weren't differences between languages, we wouldn't need more than one language in the first place. – Caleb Apr 25 '13 at 06:51
  • this answer do not make sense. In enum we can assign only int values. I know how to define constant in Objective C. But now my need is to use it globally in application. So I can use it any class – Afsal Meerankutty Apr 25 '13 at 06:55
  • If you want to use it in any class. You can import the class that has it, in your .pch file. – Rui Peres Apr 25 '13 at 06:59
  • @AfsalMeerakutty I see what you're getting at -- I've updated the answer accordingly. See if the edit helps. – Caleb Apr 25 '13 at 07:04
  • But how to define those constants in Class? We can't do the same as in c#. ELse we should set property for each and need to make instance of class to get it. How I can get it without making an instance of class. – Afsal Meerankutty Apr 25 '13 at 07:04
  • @AfsalMeerakutty You don't define the constants in a class. Classes don't have member variables, only objects do. In other languages, classes or namespaces are used to avoid name collisions. In Objective-C, name prefixes serve that purpose. – Caleb Apr 25 '13 at 07:09
  • @Caleb We should write the code for .m in init method. Right? – Afsal Meerankutty Apr 25 '13 at 07:12
  • @AfsalMeerakutty Why would you need an init method if you're not using a class? – Caleb Apr 25 '13 at 07:14
  • 1
    Oh.. now I got it. I don't want to use class same as in C#. So only want to use use some .h and .m file to define all constants as you said in your answer. I was trying to implement the same C# code in ObjectiveC and your comments made made me think :) Thanks for being kind to help me. @Caleb . – Afsal Meerankutty Apr 25 '13 at 07:25
2

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")
};
justin
  • 104,054
  • 14
  • 179
  • 226