Search code examples
iosswiftios-targets

Can you share a .swift file between an app and its extension if it relies on app-only code


I have the following set of files in my project:

Filestructurepic

The IAPHelper class uses the objective-c through a bridging header. I was wondering if I could use the IAPHelper class in both my app and its app extension like this:

(Setting IAPHelper's target membership)

IAPHelper_Membership

Without making KeychainWrapper being a member of both classes.


Solution

  • No it does not seem to work.

    I made TestClassOne

    import Foundation
    
    class TestClassOne {
    static internal func addOneOne() -> Int {
        return 2;
    }
    
    
    }
    

    TestClassOne is only part of one target.

    I made TestClassTwo

    import Foundation
    
    class TestClassTwo {
    internal static func callTestClassOne() -> Int {
    
        let i = TestClassOne.addOneOne()
        return i
    
    
    }
    
    }
    

    TestClassTwo belonged too both targets. I would keep on getting the error TestClassOne not recognized when the TestClassOne file was set to only one target but once I set the TestClassOne file to both targets that error stopped appearing.