Search code examples
iosobjective-cswiftconstantsstring-concatenation

iOS: How to get the #define from a Constant.h ObjC in a Swift File?


When I write this in the constant.h file:

#define WS_PLANNING_INFORMATION "registrations/"

It works when I call it from the Swift file.

But if I write this in the constant.h file:

#define WS_PLANNING_INFORMATION "registrations/" CRYPTOKEY

It doesn't work. The swift file doesn't see the WS_PLANNING_INFORMATION anymore.

Is there an easy way to concatenate two strings in the constant.h file to be well-retrieved by the swift file?

(The validate answer to this question is very useful and more simple than in that ticket, that doesn't suit me)


Solution

  • Use constants instead of macros.

    in constant.h

    extern const char *WS_PLANNING_INFORMATION;
    

    in constant.m

    const char *WS_PLANNING_INFORMATION = "registrations/" CRYPTOKEY;
    

    If you mean to use NSString * instead of char *

    in constant.h

    extern const NSString *WS_PLANNING_INFORMATION;
    

    in constant.m

    const NSString *WS_PLANNING_INFORMATION = @"registrations/" CRYPTOKEY;