How to store enum values in a NSMutableArray

前端 未结 6 1203
故里飘歌
故里飘歌 2021-02-06 22:58

My problem is since an enum in objective-c essentially is an int value, I am not able to store it in a NSMutableArray. Apparently NSMutableArray won\'t

6条回答
  •  攒了一身酷
    2021-02-06 23:26

    Wrap the enum value in an NSNumber before putting it in the array:

    NSNumber *greenColor = [NSNumber numberWithInt:green];
    NSNumber *redColor = [NSNumber numberWithInt:red];
    NSNumber *blueColor = [NSNumber numberWithInt:blue];
    NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:
                                 greenColor,
                                 blueColor,
                                 redColor,
                                 nil];
    

    And retrieve it like this:

    MyColors theGreenColor = [[list objectAtIndex:0] intValue];

提交回复
热议问题