4

I have two targets (Target A, Target B) and from Target B I would like to call a function which is located in Target A. I get the Use of Unresolved Identifier error when trying to compile as expected since ClassA.swift isn't part of Target B. So I tried to set the Target membership of ClassA.swift to include Target B as well but the issue with this is that ClassA.swift touches so many files and also imports different modules. It wouldn't make sense to include everything else in Target B. I also tried to set the ClassA as open by declaring it as such but that doesn't do anything (maybe I'm doing something wrong there). Not really sure what else I can try to call the function in Target A from Target B

Target A > ClassA.swift > staticFunctionA()

Target B > ClassB.swift (calls staticFunctionA())

ClassA.swift has a few functions and those functions touch a number of files. This file is a wrapper file for a lot of other things that is does. Of course if I make this file as part of Target B, I'll have to make other files part of Target B as well.

  • 1
    Extract out the function into a separate file, and include only that file in `Target A`. Additionally, the fact that your `ClassA.swift` "touches so many files" is probably a code smell, indicating that your class does too much, and needs to be broken down into smaller pieces. – Alexander Nov 13 '18 at 21:54
  • Sounds like you should look into creating a Dynamic Framework that you can move ClassA and its dependencies into. That way, you can add the framework as a dependency for both TargetA and TargetB and both can access ClassA. – AdamPro13 Nov 13 '18 at 21:59
  • @Alexander I just updated my question in regards to `ClassA.swift` and it's functionality. It touches too many files because it's the main wrapper that has affects about 10 other files which I don't believe makes sense to move to `Target B`. Separating the function into a different file (which I have already done) does nothing as it's a wrapper function. – Bhavik Patel Nov 13 '18 at 22:10
  • @AdamPro13 great thought, currently I am trying to look for a technique which would require less effort. If I can't find a solution, I will surely take the approach you mentioned. – Bhavik Patel Nov 13 '18 at 22:12

1 Answers1

3

To share the code in different targets, select ClassA.swift then check target which you would like to share ClassA.swift at "Target Membership" section

Bubu
  • 651
  • 8
  • 25
  • The issue with this is that I would need to change Target Membership of all the files that have functions that are called from `ClassA.swift`. I also have a ton of libraries that are being used across all these files. I mentioned this in my question. – Bhavik Patel Nov 14 '18 at 01:09
  • @BhavikPatel Yes you are correct, Did you get any solution to this ? – Avijit Nagare Mar 20 '22 at 16:45