[swift-users] Swift/Obj C interoperability: 'Obj C only' functions

Joe Groff jgroff at apple.com
Tue Mar 7 11:54:32 CST 2017


> On Mar 6, 2017, at 2:20 AM, Andrew Hill2 via swift-users <swift-users at swift.org> wrote:
> 
> Hi,
> 
> I’ve been looking at making some changes to the ResearchKit library. This is mainly written in Objective C, with small portions of Swift.
> 
> The Pull request I’ve generated leveraged Swift generics to make handling predicates easier from Swift.
> 
> 
> The existing functions had to utilise different functions for each type of object.
> 
> The Swift function of course uses generics and simplifies that down to a single function.
> 
> To maintain Objective C compatibility, we still need to provide the original functions for Obj C programmers. We don’t want to re-implement the functions twice in two different ways, and we want Obj C programmers to use one set of functions, and Swift programmers to only use the other one.
> 
> 
> It’s easy enough to expose a function from Swift to Objective C code of course.
> 
> However, is there a way to expose a function in a Swift file which is ONLY available externally to Objective C files and not to other Swift ones? (or at least warns if called from Swift or something like that)? Basically an @objconly ?
> 
> The case described above is fairly trivial, but there are more complicated areas where this would have a more marked effect. It would be nice not to have Swift ResearchKit developers exposed to a load of API functions they don’t need which are there only to support Obj C.

The closest thing might be to declare the methods as `@objc private`. They won't be exported to Swift, but ObjC code can still get at the method by its selector if you declare it in your ObjC headers.

-Joe


More information about the swift-users mailing list