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.

Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74
  • @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 – Sudhanshu Gupta Apr 18 '16 at 07: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? – Martin R Apr 18 '16 at 07: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 – Sudhanshu Gupta Apr 18 '16 at 08:18

0 Answers0