Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-enum style classes in Objective-C?

Tags:

objective-c

I am new to Obj-C so forgive me if this is a stupid question:

How do I implement some in the style of Javas enums? Or to be more precise:

I want a class with some known properties which are fix at compile time and unique per instance. Additionally I only want one instance type.

Let me give an example in Java:

public enum MessageTypes {
  DEFAULT("white", "standard", 1),
  EXPRESS("red", "expressMessage", 2),
  BORADCAST("green", "broadcast", 3);

  String color; String tagName; int dbId;
  MessageTypes(String color, String tagName, int dbId) {
    // you get the idea
  }
  //some methonds like getEnumByTagName
}

How would you do something like this in Objective-C? Am I missing something? Is this a bad pattern at all?

Thanks in advance!

EDIT: I am sorry, if I did not made myself clear. I know, that obj-c enums are not what I am looking for (as they are only marginally more than a typedef to an int).

I would like to create a set of (kind-of-singleton, immutable) instances of a specific class. The singleton pattern in Apples Dev-Docs is of no use as I want multiple distinct instances of a class each with individual values in their properties.

The goal of that is to have multiple Message types (about 20) that can be assigned to a Message as a property. Each of my Message types has a (fix and predefined) color, attribute-value (in an XML-representation) and a numerical ID.

In Java, I would use an enum as in my code sample. But how do I create different MessageTypes and associate them with their properties in Obj-C?

Creating 20 Sublcasses of MessageType (each with a singleton-instance holding the properties) seems like a lot of work for such a simple task and total overkill.

My current approach is to create a class with an NSArray holding the different instances. Up on first access of a method like +(id)messageTypeForId:NSInteger id_ the NSArray is prepopulated. But this feels totally clumsy and not at all elegant...

Is there a more satisfying approach?

like image 466
Mo. Avatar asked Jun 10 '09 17:06

Mo.


People also ask

What is enum in Objective C?

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code. If you are familiar with C, you will know that C enumerations assign related names to a set of integer values.

Can java enum have methods?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.


2 Answers

There is not much in the way of a "more satisfying approach".

The normal Cocoa pattern would be to create methods like:

+ (MessageTypes*) sharedDefaultMessageType;
+ (MessageTypes*) sharedExpressMessageType;
+ (MessageTypes*) sharedBroadcastMessageType;
etc

and then implement them something like:

+ (MessageTypes*) sharedDefaultMessageType
{
   static MessageTypes* thisMessageType = nil;
   if ( !thisMessageType ) {
      thisMessageType = [[MessageTypes alloc] initWithColor:@"white" tagName:@"standard" dbId:1];
   }
   return thisMessageType;
}

Alternatively, storing the shared MessageType* in an NSMutableArray or NSMutableDictionary or precalculating them as you are doing are all equally valid approraches.

Note that the above "template" method could be generated via a macro such that you could write in the .m file:

CREATEMESSAGETYPE( Default, @"white", @"standard", 1 )
CREATEMESSAGETYPE( Express, @"red", @"expressMessage", 2 )
CREATEMESSAGETYPE( Broadcast, @"green", @"broadcast", 3 )

which might be "more satisfying" or more ugly, depending on your point of view.

like image 131
Peter N Lewis Avatar answered Nov 02 '22 21:11

Peter N Lewis


I think I'd just use a standard C enum:

typedef enum { MT_WHITE, MT_RED, MT_GREEN } MessageType;

Then you just use it as you would any other data type:

@interface Blah {}

-(void) setMessageType:(MessageType)newMessageType;

@end
like image 1
Stephen Darlington Avatar answered Nov 02 '22 20:11

Stephen Darlington