Files in Swift Package out of scope

3.7k views Asked by At

I created a basic project with a swift package(let's call it "Foo") related to this project.

I tried printing "hello world" from Foo().text from AppDelegate and I get an error:

"Cannot call value of non-function type 'module<Foo>' "

I then tried to add a new struct or class to this package and I get an error: "Cannot find {ObjectName} in scope" Tests inside Foo all passed. It looks like I missing some step(s).

2

There are 2 answers

2
matt On BEST ANSWER

Don't confuse a module with a public class (or struct). That's what it sounds like you are doing here:

I create a basic project with a swift package (let's call it "Foo") related to this project and try print "hello world" from Foo().text

Saying Foo() implies there is a public class/struct called Foo. Is there? I don't think so. You might have a package called Foo, which you import. But that does not mean that the Foo package has a public class (or struct) called Foo.

For example you might see this in your package:

struct Foo {
    var text = "Hello, World!"
}

But that does not mean you can simply import Foo and then talk about Foo().text, because this Foo is not public.

5
Lemon On

You might of forgotten to import your Swift Package into AppDelegate. Try putting the equivalent for your package in the top of AppDelegate.

import Foo

According to this Github Issue, (if you're using CocoaPods, which you haven't stated in your question.) You will have to make the class/struct in your package public.

You might also have to try like this:

Foo.bar()

Foo being your package name, and bar being the class or struct inside it.