Trond Kristiansen Trond Kristiansen - 4 days ago 5
Objective-C Question

Access #define constant array in Swift

I store constants in a objective-c header file as suggested here. I define a NS_ENUM and also a constant array which works nicely in objective-c e.g. SDModeString(kSDOne). However, I cant get this to work called from Swift. I am sure I am doing something very stupid here, but a suggestion for how to call SDModeString from Swift would be appreciated. I can get access to SDMode but not SDModeString. Thanks.

typedef NS_ENUM(NSInteger, SDMode) {
kSDOne, kSDTwo, kSDThree
#define SDModeString(SDMode) [@[@"kSDOne",@"kSDTwo",@"kSDThree"] objectAtIndex:SDMode]

hnh hnh

You need to turn SDModeString into a proper function, like so:

inline NSString *SDModeString(enum SDMode mode) {
  return [@[ @"kSDOne", @"kSDTwo", @"kSDThree" ] objectAtIndex:mode];

though I would probably just use a switch, faster, safer etc:

inline NSString *SDModeString(enum SDMode mode) {
  switch (mode) {
    case kSDOne:   return @"kSDOne";
    case kSDTwo:   return @"kSDTwo";
    case kSDThree: return @"kSDThree";
    default: assert(false);