skip to Main Content

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

Edit:

The project I created was a Command Line Tool, with Swift selected as the language. All other settings were defaulted (at least I believe so)

Also, restart Xcode or create a new project wouldn’t solve the problem.

2

Answers


  1. Chosen as BEST ANSWER

    Turns out the problem is probably because I have the project named Swift.

    Not sure the exact reason behind that, but the name Swift probably have caused certain problems.


  2. 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).
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search