enum Values to NSString (iOS)

前端 未结 16 1911
悲&欢浪女
悲&欢浪女 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条回答
  •  Happy的楠姐
    2020-12-04 08:17

    I will introduce is the way I use, and it looks better than previous answer.(I thinks)

    I would like to illustrate with UIImageOrientation for easy understanding.

    typedef enum {
        UIImageOrientationUp = 0,            // default orientation, set to 0 so that it always starts from 0
        UIImageOrientationDown,          // 180 deg rotation
        UIImageOrientationLeft,          // 90 deg CCW
        UIImageOrientationRight,         // 90 deg CW
        UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
        UIImageOrientationDownMirrored,  // horizontal flip
        UIImageOrientationLeftMirrored,  // vertical flip
        UIImageOrientationRightMirrored, // vertical flip
    } UIImageOrientation;
    

    create a method like:

    NSString *stringWithUIImageOrientation(UIImageOrientation input) {
        NSArray *arr = @[
        @"UIImageOrientationUp",            // default orientation
        @"UIImageOrientationDown",          // 180 deg rotation
        @"UIImageOrientationLeft",          // 90 deg CCW
        @"UIImageOrientationRight",         // 90 deg CW
        @"UIImageOrientationUpMirrored",    // as above but image mirrored along other axis. horizontal flip
        @"UIImageOrientationDownMirrored",  // horizontal flip
        @"UIImageOrientationLeftMirrored",  // vertical flip
        @"UIImageOrientationRightMirrored", // vertical flip
        ];
        return (NSString *)[arr objectAtIndex:input];
    }
    

    All you have to do is :

    1. name your function.

    2. copy contents of enum and paste that between NSArray *arr = @[ and ]; return (NSString *)[arr objectAtIndex:input];

    3. put some @ , " , and comma

    4. PROFIT!!!!

提交回复
热议问题