Why can I not use my constant in the switch - case statement in Objective-C ? [error = Expression is not an integer constant expression]

前端 未结 2 2122
难免孤独
难免孤独 2020-12-18 22:06

So I have an issue with using a constant variable in the following switch statement in Objective-C.

I have Constants.h with the following:



        
2条回答
  •  情书的邮戳
    2020-12-18 22:19

    I would normally follow what Apple seem to do and define a typedef enum in the .h file like this.

    typedef NS_ENUM(NSInteger, PSOption) {
      PSOption1,
      PSOption2,
      PSOption3,
      PSOption4,
    };  
    

    You can then use it in your case statement and even pass it into functions as well as a type e.g.

    - (void)myMethod:(PSOption)option;
    

    A further advantage of doing this over a #define is code completion and compiler checking

提交回复
热议问题