Enumerations in Objective-C

I am making a switch from Java to Objective-C. I am wondering if there is a concept similar to Java enumerations that supports method implementations. I understand that Objective-C has simple old C listings, but they are really just ints.

I am looking to prevent switch shortcuts - Java enums would be ideal.

+3
source share
1 answer

Objective-C is just C with some extra markup for objects, no new types added.

That means no.

For mutually exclusive flags, Apple uses strings.

header.h

extern NSString * const kNSSomeFlag;
extern NSString * const kNSOtherFlag;
extern NSString * const kNSThirdFlag ;

code.m

NSString * const kNSSomeFlag = @"kNSSomeFlag";
NSString * const kNSOtherFlag = @"kNSOtherFlag";
NSString * const kNSThirdFlag = @"kNSThirdFlag";


void myFunction(NSString *flag)
{
    if (flag == kNSSomeFlag) {
        // the code
    }
}

An example of this can be found in NSDistributedNotificationCenter.

+6
source

Source: https://habr.com/ru/post/1727281/


All Articles