0

There is a thing that while defining macro's i was using a objective c file. Lets say i define a macro as

#define TRY_THIS  @"try_this"

Then i am using this macro to save a string in Swift file in defaults

NSUserDefaults.standardUserDefaults().setObject("12", forKey: TRY_THIS )

As i know, while compiling, macros just replace the TRY_THIS to @"try_this". Lets say it is replaced :-

NSUserDefaults.standardUserDefaults().setObject("12", forKey: @"try_this" )

But "@" is not used in swift , so should this give me error or not.

I am not sure. Can anyone tell me how to use a hash define strings of objective c in swift.

I am seeing that this command is working . Can you let me know why?

Also to get the value back i am using :-

NSUserDefaults.standardUserDefaults().stringForKey(TRY_THIS)

NSUserDefaults.standardUserDefaults().stringForKey("try_this")

Both should work while getting value back. If yes why. if not why ?

Please clarify my confusion.

3
  • @EICaptain, I know this will work but i am maintaining an objective c .h file for constants, i don't want to change it to swift Commented Apr 18, 2016 at 7:13
  • 1
    If #define TRY_THIS @"try_this" is in an Objective-C header file which is included from the bridging header file, then it is imported to Swift as a String variable. So NSUserDefaults.standardUserDefaults().setObject("12", forKey: TRY_THIS ) should just work. – What is your problem? Commented Apr 18, 2016 at 7:17
  • @MartinR Can you please let me know why is it working and I have changed the question a bit . Can you look at it again Commented Apr 18, 2016 at 8:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.