ibnetariq
ibnetariq

Reputation: 738

Swift Package imported to specific file only

I have added Stevia using Swift Package Manager as recommended. I am already using Cocoapods for other dependencies.

Right now I have a separate swift file in which I am importing Stevia for use in extension like this

import UIKit
import Stevia

extension UIView {
    func vHeight(_ points: CGFloat) {
        self.height(points)
    }
    
    func vWidth(_ points: CGFloat) {
        self.width(points)
    }
}

Problem is that this makes Stevia available in whole project. For example if I got to new ViewController and there is no

import Stevia

, I can still access it. I wan't to get rid of this behaviour. Is it possible?

Upvotes: 7

Views: 476

Answers (1)

iUrii
iUrii

Reputation: 13768

Unfortunately it is not possible due the known longstanding compiler bug: [SR-3908] Extension methods visible without importing module in a file (https://github.com/apple/swift/issues/46493).

Once you added import Stevia in your project all public extentions from this module (swift package) become available in your whole module (your app) like functions that you use from UIView extension:

// Stevia+Size.swift

public extension UIView {
...
@discardableResult
    func height(_ points: CGFloat) -> Self {
        height(Double(points))
    }
...
}

But it doesn't work with types! Types from the other module (swift package) are not accessible without import statement in an each file where they are needed:

//import Stevia

let space = FlexibleSpace() // Error: Cannot find 'FlexibleSpace' in scope

So this is essence of the bug.

Upvotes: 4

Related Questions