Swift/Obj C interoperability: 'Obj C only' functions


(Andrew Hill2) #1

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.

Best wishes,

Andrew---------------------------------------------------------------------------------------------------------------------
Virus Scanned and Verified by St Helens and Knowsley Health Informatics

···

---------------------------------------------------------------------------------------------------------------------

DISCLAIMER: This e­mail may contain confidential and/or proprietary information some or all
of which may be legally privileged. It is for the intended recipient only. If any addressing or
transmission error has misdirected this e­mail, please notify the author by replying to this e­
mail and destroy any copies. If you are not the intended recipient you must not use, disclose,
distribute, copy, print, or rely on this e­mail.

The information contained in this email may be subject to public disclosure under the
Freedom of Information Act 2000. Unless the information is legally exempt from disclosure,
the confidentiality of this email AND YOUR REPLY cannot be guaranteed.


(Joe Groff) #2

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

···

On Mar 6, 2017, at 2:20 AM, Andrew Hill2 via swift-users <swift-users@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.