1

I have a Framework with has one Objective-C class with one designated initializer which takes two NSArrays. Inside the Framework, I have defined a Swift extension which provides an extra initializer which takes an array of tuples instead of the two arrays.

When importing the Framework eternally, is it possible to hide the original Objective-C initializer from Swift (so only the initializer taking the array of tuples can be used) but keep it available when using the Framework from Objective-C code?

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92

2 Answers2

3

Answer by @mattt:

Use the NS_SWIFT_UNAVAILABLE macro (available only on Xcode 7 and up).

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
2

You could:

YourApp-Brindging-Header.h

#define __BRIDGING__
#import "YourObjCObject.h"
#undef __BRIDGING__

YourObjCObject.h

#import <Foundation/Foundation.h>

@interface YourObjCObject : NSObject

@property (assign, nonatomic) NSInteger count;

- (instancetype)initWithArray:(NSArray *)ary1 Array2:(NSArray *)ary2 NS_DESIGNATED_INITIALIZER
#ifdef __BRIDGING__
NS_UNAVAILABLE
#endif
;
@end
rintaro
  • 51,423
  • 14
  • 131
  • 139
  • BTW, if you hide *designated initializer* from Swift, how do you implement *convenience intializer* in Swift extension? – rintaro Aug 19 '15 at 13:48
  • Yeah, that's part of my problem. I want to use it on my own convenience initializer, but I want to hide it elsewhere. If that's not possible, I can reimplement the *designated initializer* from Swift anew. Also, an additional issue, this is inside a Framework, which doesn't have a bridging header (as described [here](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html)). Can my requested behaviour be achieved for the users of the framework? – Ricardo Sanchez-Saez Aug 19 '15 at 14:09
  • Or in other words, can *usage from Swift* be detected from the preprocessor macro without manually defining `__BRIDGING__ ` as in your solution? – Ricardo Sanchez-Saez Aug 19 '15 at 14:14
  • So, this QA might help you? [How to import private framework headers in a Swift framework?](http://stackoverflow.com/q/28746214/3804019). – rintaro Aug 19 '15 at 14:20
  • Thanks, that's promising! However, I cannot still see a way to do what I wanted from outside the *Framework*: have the *Obj-C* initializer available from *Obj-C*, but have only the *Swift* initializer available from *Swift*. I have updated the question to reflect this. I'll read the [Clang Modules documentation](http://clang.llvm.org/docs/Modules.html), to see if I can find anything suitable. – Ricardo Sanchez-Saez Aug 19 '15 at 14:48