0

I have a common constant file which will have all my objective C macros, to access my objective c macros in swift class I have planned to implement the way suggested in the following article.

Way To Access Objective C macros In Swift

Basically, what has suggested is create .h & .m file and add public methods which return the macro we want to get access too.E.g

I have an objective C macro to get the custom color

#define THEME_COLOR [UIColor colorWithRed:115.0/255.0 green:190.0/255.0 blue:123.0/255.0 alpha:1.0f] // this macro is not accessible inside swift class 

So I am creating a method as follows

+ (UIColor *) themeColor {
   return THEME_COLOR;
}

Now in my swift class, I am able to access this method. Since the above method is declared in a global class all my objective C classes are also accessing this method. I want to give access to this method only if it is accessed from swift classes is there any way to do it? Or any other better approach available?

Any suggestion or help greatly appreciated. Thanks In Advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Peer Mohamed Thabib
  • 636
  • 2
  • 9
  • 29

1 Answers1

2

Don't import the .h file that defines +themeColor into your ObjC code. Just put it in your bridging header to Swift.

(That said, I wouldn't bother with this. I'd highly recommend just getting rid of the macros and using the method everywhere. And if you don't want to get rid of the macro, I still wouldn't hide the method from ObjC. Methods are a generally better solution.)

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Your answer did the trick! Thanks,@Rob Napier . I am working on the already developed project and it has several macros, converting every thing on the go would be time-consuming work. Several people are working in this code base so with out restricting it would make the code clumsy over the period of time – Peer Mohamed Thabib Aug 28 '17 at 12:43