Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transitive imports in consumers of Swift frameworks

Tags:

Let's say I have framework that defines a protocol which depends on symbols exported by a 3rd framework:

import CoreLocation

public protocol BarsAPIClient {
    func getBars(around location: CLLocation, completion: @escaping (Result<[String], Error>) -> Void)
}

Now, in my app I want to add a concrete implementation of the protocol:

import MyFramework

class BarsAPIClientImpl: BarsAPIClient {
    func getBars(around location: CLLocation, completion: @escaping (Result<[String], Error>) -> Void) {
        // actual implementation goes here
    }
}

However, the above code won't compile due to CLLocation not being visible:

enter image description here

I can easily address this particular error by also importing CoreLocation. However for more complex cases, with multiple dependencies, this might become tedious.

So, question is if it's possible for the module to declare all its public dependencies, so consumers of that module are automatically linked to those dependencies?

like image 753
Cristik Avatar asked Jan 13 '20 12:01

Cristik


1 Answers

There are currently 2 ways to do this

  1. You can use an undocumented swift feature for this, which can break in the next swift version.

Add this to a swift file in MyFramework.

@_exported import CoreLocation
  1. You can use current behavior of objc-swift interop which works the same way.

Create MyFramework.h public umbrella header in MyFramework with the following contents.

#import <CoreLocation/CoreLocation.h>
like image 72
VoidLess Avatar answered Sep 30 '22 21:09

VoidLess