Declaring extern NSString causes linker error

后端 未结 4 1649
旧巷少年郎
旧巷少年郎 2021-02-07 11:17

This is ridiculous, im trying to create a sound bool to turn of in app sounds. I keep getting

Undefined symbols for architecture i386:
\"_kPlaySoundPrefsKey\",         


        
4条回答
  •  深忆病人
    2021-02-07 12:18

    When you declaring something as extern you are telling the compiler the type AND that you will define it somewhere else. In your case you never define your variable.

    So in your .h you would have:

    extern NSString* kPlaySoundPrefsKey;
    

    But in some .m you must also have

    NSString* kPlaySoundPrefsKey = @"play_sounds"; // or some value
    

    Since in your case these are constants you can also specify:

    extern NSString* const kPlaySoundPrefsKey;
    

    and

    NSString* const kPlaySoundPrefsKey = @"play_sounds"; 
    

    The addition of the const qualifier will cause a compiler error if someone ever writes something like

    kPlaySoundPrefsKey = @"some_other_value";
    

提交回复
热议问题