0

I have made an Xcode Swift project ("Project1"). In a new project ("Project2"), I have trouble adding project 1.

I have tried adding project 1 to project 2's build phases (target dependancies, compiled sources, link binary with libraries); didn't work. When adding to the compiled sources, it wouldn't work no matter which option I chose (folder references, groups, copy if needed).

I get no compiler errors at:

import Project1

But when I try to use a class from project 1, I get the error "Use of undeclared type".

I have also tried to following links with no success:

Both projects are in Swift (iOS).

I'd be very thankful if someone helped me with this issue.

Update: Project 1 is not a framework - it's an iOS app. I need to use some of its classes in project 2. The problem is that project 1 uses the Objective C library Common Crypto via a bridging header. When I manually add project 1 classes into project 2, I get an error ("unresolved identifier") in the project 1 Swift code that uses Common Crypto.

So in a nutshell: I have an iOS app (project 1), which is in Swift but uses Common Crypto via bridging header. When I add a number of classes from project 1 into project 2, it cannot resolve the references (in project 1) to Common Crypto variables.

Community
  • 1
  • 1
user3664254
  • 163
  • 1
  • 7
  • Could this be something to do with access control levels (private, public and so on in your Project1)? Could you add some code showing us how you try to use something from Project1 in Project2 maybe? – pbodsk Nov 01 '16 at 10:01
  • Have a look at Heimdall.swift, it also makes use of CommonCrypto and has a buildphase script that you can easily import into Project2 to solve this issue. I have a similar setup that does this. The only other way you could solve this then is using an umbrella framework which isn't that nice. Link: http://pastebin.com/1vmiqffu – Antwan van Houdt Nov 01 '16 at 11:18

2 Answers2

0

Edit: As you have CommonCrypto as a dependency you will have to add the module to your Project2 project instead to solve your issues ( this is the easiest without resorting to an umbrella framework ). Add a run script build phase and include http://pastebin.com/1vmiqffu

-- Credits: Script 'stolen' from: https://github.com/henrinormak/Heimdall

Ok so I'm going to assume here that Project1 actually has a framework as a target. What are the access permissions set on the types you are trying to use?

Here are a couple of catchya's with Swift and frameworks as I encountered them:

  • You do not have a bridging header, instead your framework includes headers of non-Swift dependencies inside the header file of your framework ( ModuleName.h ). This also means these will be available to whatever project you import them to. As far as I know you need to use a module.modulemap in order to make use of private headers and includes.
  • All Swift Classes / Structs / Definitions in general are internal by default. It is a very good design choice and it forces you to think about the access rights on every component you write. Keeping things private by default makes it easier to only open stuff that really needs to be open ( public, open ), allowing for easier code maintenance since you know that private things are only accessed within the same context. ( Otherwise: error )

For some more assistance this link might be of help to you on how to do some fundamentals:

your first ios framewok (swift)

Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52
0

Assuming Project1is a Framework and Project2 is an application using the framework:

  • Create a virgin Workspace (Xcode File -> new -> Workspace) named TestWorkspace
  • From the Finder, drag the Project1.xcodeprojfile to the TestWorkspace
  • From the Finder, drag the Project2.xcodeprojfile to the TestWorkspace, above Project1

Edit your TestWorkspace schemas Build setup:

  • Add Project1 and Project2
  • make sure Project1 is above Project2
  • Untick "Paralellize Build" to assure Project1 is build first
  • Build
  • Select Project2s target -> General
  • Drag artefact project1.framework(in Productsgroup) to "Linked Framworks and Libraries"

Note: To be visible for the client, all classes and methods in your project1.framework have to be public or open. Finde detailed information in Apples documentation.

shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • Project 1 isn't a framework. It's an iOS app, I want to use some of its classes. Project 1 is entirely in Swift, but it uses an Objective C framework (common crypto) via a bridge header. I could add all the classes into project 2, but then it would show the "unresolved identifier" error in the Swift class in project 1 where common crypto is used. – user3664254 Nov 01 '16 at 10:39
  • Ok. I misunderstood though. I recommend to refactor the the cryto stuff out to a framework then. – shallowThought Nov 01 '16 at 10:42
  • isn't there a simpler way? – user3664254 Nov 01 '16 at 10:45
  • This is quite simple and the only way I can recommend – shallowThought Nov 01 '16 at 11:13
  • You can certainly copy the files to reuse to Project2 and add your Objective-C crypto framework like you did in Project1. If you have an issue with that, search for a solution (as it does not fit to the question here) and if you do not find one describe your setup and the issue as detailed as possibe in a new question. – shallowThought Nov 01 '16 at 13:06
  • Tried refactoring into a new project. Didn't help. I get an error when importing common crypto: http://stackoverflow.com/questions/40361422/cannot-import-common-crypto-in-a-swift-framework – user3664254 Nov 01 '16 at 13:51