Reputation: 400
I have an Objective-C class that I am trying to use in a Swift project.
Some of the methods in this class are not suitable for Swift and the simplest solution in my opinion is to have slightly different alternatives that will work in Swift.
eg. Current Method:
+(void)doSomethingWithFormat:(NSString*)format,...;
eg. New Method:
+(void)doSomethingWithString:(NSString*)string;
That works fine but my problem is the new method will then be available in Objective-C projects where it isn't really needed and just clutters up the code complete menu.
My question: Is there a way to mark all of the new methods as only being available in Swift and not in Objective-C?
EDIT: What I'm hoping for is something along the lines of
@available(swift,*)
+(void)doSomethingWithString:(NSString*)string;
or
#if SWIFT
+(void)doSomethingWithString:(NSString*)string;
#endif
Upvotes: 3
Views: 983
Reputation: 53010
What you are looking for is the opposite of NS_SWIFT_UNAVAILABLE
which Apple provide to make an Objective-C API unavailable to Swift. There is an NS_UNAVAILABLE
which makes the API unavailable in both, bot not an NS_OBJC_UNAVAILABLE
.
End of Answer. I will deny posting the following ;-)
In the following Objective-C sees bar
, Swift sees bar()
and foo()
...
@interface Foo : NSObject
- (void) bar;
- (void) /*__*//*__ between these comments there is a zero-wdith non-joiner character U+200D */ NS_SWIFT_NAME(foo());
@end
Yes, it seems you can name a method in Objective-C using the zero-width non-joiner character. You won't see this in auto complete, you'll see a blank!
I will now face the corner and repeat "I will not give methods invisible weird names" 1000 times ;-)
Upvotes: 4
Reputation: 27221
I suggest using an extension of ObjC class in Swift-file.
I mean, in ObjC create your class MyClass: NSObject
...
in Swift file use:
extension MyClass { /* your methods */}
Upvotes: 1