-2

I have 2 big projects (ProjectA & ProjectB) in a workspace. I need to access multiple files from ProjectA into ProjectB. Example: I have a file named Test.h & Test.m & Test.hpp & Test.cpp in project A. I would like the target from the project B could see it. For now, all the classes from the project A are not accessible from the target of the projet B. Each project are separated as if they cannot see each other.

I want to use them as a separate projects instead of library.

But I dont want the same file be in two projects as I use this projects in many other projects, I don't want redundancy as it take lots of space.

enter image description here

How can I achieve this please?

ΩlostA
  • 2,501
  • 5
  • 27
  • 63

1 Answers1

1

I would personally try and extract those files into a package or a framework and then use that package or framework in both of your files.

But there may be a less beautiful alternative. You can drag your files to Build Phases under Compile Sources of your target project. So if you have a file MyFile.swift in Project A but want to also use it in Project B do the following:

  • Select Project B in your navigator
  • Select Build Phases tab
  • Extend a section Comple Sources
  • Drag MyFile.swift from navigator to Compile Sources
  • Deselect "Copy items if needed" and confirm (Use whatever other options you want)

This will now show the same file under both of the projects in navigator. It looks as if it was a copy of a file but it is the same file; by changing one you are changing the other.

You can do the same with resources but remember to drag them under Copy Bundle Resources instead of Compile Sources.

But again think about creating local packages as they are also made in few clicks.

  • File -> New -> Package
  • Name it and select your framework under Add to:
  • Drag your files to package and add public for what you need to expose
  • Select your target project in navigator
  • Open General tab
  • Under Frameworks, Libraries, and Embedded Content select + button
  • Click your package and add it
  • Remember to use import <#PackageName#> in source code to use your package
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • thank you for your very exhaustive answer. I tried the less beautiful alternative, but I have lots of problems with adding the libraries, and adding all the references file to the project... – ΩlostA Apr 14 '23 at 15:46