Xcode: "<unknown>:0: error: cannot load module ‘main’ as ‘Swift’" upon trying to compile

Published

Just started using Swift on Xcode.

For some reason, anytime I want to compile, it shows the error: <unknown>:0: error: cannot load module 'main' as 'Swift'

The only way I could find to fix it is to either clean build folder or delete build folder myself, then compile.

The code I have is just changing from:

print("Hello, world!")

to

print("Hello, world")

Am I doing something totally wrong?

Xcode version: 12.1 (12A7403) with Xcode 12.1 Toolchain

Source: Swift Questions

Published
Categorised as swift, xcode, xcode12 Tagged , ,

Answers

Ideally what you are doing shouldn’t change anything at all, since all you are doing is changing a string. I have had this happen to me before where changing some arbitrary code drastically changes the compilation of the program, but it usually goes away after restarting my IDE or rewriting the code.

The first thing I would check is your project name: If the name of your project is ‘main,’ the compiler will confuse the main module with your target name (your target name in this instance is the same as your project name)

There are a few things you could do to troubleshoot:

  1. Completely restart Xcode and reopen the project.
  2. Delete the file and create a new one with the same code.
  3. Change the code back to print("Hello, World!") and see if the code starts working again.
  4. If the above options don’t work, try deleting the derived data folder (sometimes it can act up, causing build-time errors for no reason).
  5. If deleting derived data doesn’t work, open a new project and try to change print("Hello, World!") to print("Hello, World") again, and see if anything changes. You might have a corrupt file in your initial project. (Based on your question it seems like you have already done this, but try again after deleting derived data anyways).

Ella Schimmel

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq