enum Values to NSString (iOS)

前端 未结 16 1949
悲&欢浪女
悲&欢浪女 2020-12-04 07:57

I have an enum holding several values:

enum {value1, value2, value3} myValue;

In a certain point in my app, I wish to check which value of

16条回答
  •  孤城傲影
    2020-12-04 08:19

    This is answered here: a few suggestions on implementation

    The bottom line is Objective-C is using a regular, old C enum, which is just a glorified set of integers.

    Given an enum like this:

    typedef enum { a, b, c } FirstThreeAlpha;
    

    Your method would look like this:

    - (NSString*) convertToString:(FirstThreeAlpha) whichAlpha {
        NSString *result = nil;
    
        switch(whichAlpha) {
            case a:
                result = @"a";
                break;
            case b:
                result = @"b";
                break;
            case c:
                result = @"c";
                break;
    
            default:
                result = @"unknown";
        }
    
        return result;
    }
    

提交回复
热议问题