Module from sub-project (framework) can't be found
Asked Answered
P

3

8

I am using XCode 8 + Swift 3.

I created a fresh iOS project named "MyApp". Then, I create a Cocoa touch framework project, named "MySubProject". (The idea is to have MyApp project accessing MySubProject code.)

They are on the same level folder:

- MyApp/
- MySubProject/

In MySubProject, I have a MyService.swift file (it is MyService class) under service group/folder:

enter image description here

The code is very simple:

import Foundation
public class MyService {
    public func greeting() -> String {
        return "Hello my service!"
    }
}

In MyApp, I added MySubProject.xcproject into MyApp project as a sub-project under MyApp:

enter image description here

Then, I drag and dropped the MySubProject.framework under MySubProject/Products/ into MyApp project's General --> Embedded Binaries and Linked Framework and Libraries:

enter image description here

Now, under MyApp/ViewController.swift, I import the MyService class from MySubProject: enter image description here

But when I build MyApp project, I always get compiler error "No such module 'MyService'" as you see in above picture.

Why? How to make MyApp being able to access MySubProject code?

Priscian answered 18/7, 2017 at 16:8 Comment(1)
I am surprised that no one knows how to fix it???Priscian
S
2

import MySubProject ! not myService...after that copy framework to the app, see the snapshot, please. I can't post image. snapshot

Superclass answered 24/7, 2017 at 2:11 Comment(2)
Thanks. Actually,the only problem is that I should import MySubProject instead of MyService. I don't need to copy the framework, since I already added the framework in "Embedded binary" and "Linked frameworks and Libraries", if you see my screenshot in my question.Priscian
Having read once more your question, I think hasayakey is right. One had to import the framework, not the file. Please try it.Wynellwynn
R
0

Does MySubProject have the "Defines Module" setting enabled?

Ramsgate answered 18/7, 2017 at 22:25 Comment(3)
Where is "Defines Module" setting?Priscian
Yes, it is enabled.Priscian
Check the Product Module Name setting for your subproject. That needs to match whatever you use in the import command. So if it's defaulting to MySubProject, and you're trying to import MyService, it won't find it. You can either import MySubProject, or change the Product Module Name to MyService - just so long as they match.Ramsgate
A
0

EDIT:
There are several things you can check:

To be found in your main project, your framework has to be compiled to the same target, i.e. if your main project has iOS as target, your subproject must also have iOS as target (and not, e.g. watchOS).
Activate in the project navigator the project. This shows a pane with project and target settings. Activate your target and select the General tab. Under Deployment Info, you can check the target type.

To be imported in your main project, your framework must be built when your main project starts building, i.e. this must be done in the right sequence.
This sequence can be chosen appropriately: In the top bar of the Xcode window (the one with the red/yellow/green window control buttons leftmost), right of the stop button is a field with the active scheme left and the chosen device right. Clicking the scheme opens a submenu. Select Edit scheme (this can also be done from the Xcode main menu by Product/Scheme/Edit scheme). Select Build in the left pane. On the right side, you will see your targets. They will be built from top to bottom. Therefore, your framework must be on top, since your main project needs it for import when it starts building. You can change the sequence by dragging the targets accordingly.

There is one more thing: Above the targets, there is a check box "Parallelize Build". This must not be checked! If it is checked, your main project and your subproject could be built at the same time, which won't work, since the subproject must be compiled completely when the main project starts building.

And, eventually, you have to import your framework (as hasayakey suggested in this answer, +1), and not a specific file of the framework).

Previous suggestion:
Has your subproject file „MyService“ (or however it is called) enabled target membership for „MyService“?

Abcoulomb answered 23/7, 2017 at 18:54 Comment(3)
It is my first time hear the term "target membership", where is it & how to enable that?Priscian
In Xcode Project Navigator (left pane), activate "MyService.swift" (blue background). Open the Utilities pane right (top bar, rightmost button). The pane will then show "Target Membership" with checkboxes. The box for your target must be checked, otherwise MyService would not be included in your target, and could not be found.Wynellwynn
Thanks. It is checked. No problem there. Target Membership is "MySubProject"Priscian

© 2022 - 2024 — McMap. All rights reserved.