HELP:swift can not import custom class?

when I use Swift, In current file for example main.swift, I import a class defined in an other file, for example B.swift in the sub-folder which is in the same folder with main.swift.The instruction of catalog is main.swift and sub-folder/B.swift.I always receive an error :“no such module”, why???

When you try to compile multiple files on replit, you need to manually add other files to the compile configuration.

To do so, on the file directories, there’s a three dot icon you can click, where you can select Show hidden files, which will bring a file called .replit, where you can append other files needed to the compile:

compile = ["swiftc", "-o", "main", "main.swift", "your_other_file.swift"]

In your case the compile would be:

compile = ["swiftc", "-o", "main", "main.swift", "SubFolder/B.swift"]

Another thing is that you are defining the struct BB inside another struct B in B.swift but you are trying to initialize it using SubFolder.B.BB(). SubFolder here is being treated as a module but it’s actually a directory.

If you want to keep B as a sub-struct of BB, you should initialize it like this::

var b = B.BB()

To keep your directory structure and file structure separate, then your B.swift file would look something like this::

public struct B {
    public struct BB {
        public var ii: Int = 0

And by now you should be able to access B.BB() from main.swift .

1 Like

Thank you very much!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.