I have added Project B into Project A as sub project. Directly drag and dropped to the Project A. I have some classes in Project B like Class X, Class Y. I need to access these Project B classes Class X, Y in project A. When I directly use the classes, Im getting Class not found error. When I tried to add Project B name as Mobule name,Im getting Module not found error. Same can work in Objective C , by giving #import "ClassX.h". But how to do it in Swift project?
Asked
Active
Viewed 2,837 times
1
-
I think you would do better with a bit of refactoring - create a Framework target. See my detailed answer on doing this in this question: http://stackoverflow.com/questions/41207622/how-to-make-same-ios-swift-app-with-only-some-differences-in-the-code-and-assets/41209283#41209283 – Jan 11 '17 at 13:31
1 Answers
-1
Step 1 : Click on that Swift file.
Step 2 : Go to File Inspector
(First tab in right panel)
Step 3 : Tick on all required targets (This will give you access of that file to all ticked targets)
Step 4 : Enjoy :)
Else,
Step 1 : Click on SubProject on which you want to add that file ClassX
.
Step 2 : Go to it's Build Phases
(6th tab)
Step 3 : Click on +
under Compiler Sources
Step 4 : Click Add Other...
Step 5 : Choose file ClassX
and finish.

Mohammad Zaid Pathan
- 16,304
- 7
- 99
- 130
-
The targets are shown only for Project B, it is not showing Project A target. Without adding targets, can we do it. Like adding Imports? – bharath gangupalli Jan 11 '17 at 11:13
-
@bharathgangupalli please refer update answer's else section. – Mohammad Zaid Pathan Jan 11 '17 at 11:25
-
Thanks Zaid. But I need it to be loosely coupled. I should be easily move the sub project to different repository. So, I can't add the dependencies. For eg. In Objective C, we can do #import "ClassX.h" in parent project to use the class. Similarly can we have for Swift? – bharath gangupalli Jan 11 '17 at 11:31
-
@bharathgangupalli If you want to make that `ClassX` independent of both projects then you could make framework of it and then import that framework in both projects whenever needed. (Note : This will save your compile time). Sorry but I did not understand your last statement. – Mohammad Zaid Pathan Jan 11 '17 at 11:38
-
ClassX belongs to Project B which is a Sub project. I added this sub project (Project B) to Project A. Now I need to access Class X from Project A. (Class X is part of Project B) – bharath gangupalli Jan 11 '17 at 11:46
-
Yes so the approach described in Else part could be used or your could make Framework as described earlier. Without linking `ClassX` to your target you can not get access of it within code. – Mohammad Zaid Pathan Jan 11 '17 at 11:58