How to make my Objective C class method only visible for Swift classes

437 Views Asked by At

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

1

There are 1 best solutions below

1
On BEST ANSWER

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.)