“sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers” warning

后端 未结 3 825
遥遥无期
遥遥无期 2021-01-29 16:51

I have Constants NSString, that I want to call like:

[newString isEqualToString:CONSTANT_STRING];

Any wrong code here?

I got this warni

3条回答
  •  孤独总比滥情好
    2021-01-29 17:47

    just to put all on one place which found on various post on stackoverflow and works for me , #define is bad because you cannot benefit from variable types, basically the compiler replaces all occurrence when compiles (import Constants.h whenever you need) :

    //  Constants.h
    #import 
    
    @interface Constants : NSObject
    
    extern NSString *APP_STATE_LOGGED_IN;
    extern NSString *APP_STATE_LOGGED_OUT;
    @end
    
    // Constants.m
    #import 
    #import "Constants.h"
    
    @implementation Constants
    
    NSString *APP_STATE_LOGGED_IN  = @"APP_STATE_LOGGED_IN";
    NSString *APP_STATE_LOGGED_OUT = @"APP_STATE_LOGGED_OUT";
    @end
    

提交回复
热议问题