38

I'm trying to run the Automatic Preview in Xcode 11, however even though the project builds successfully, the automatic preview fails giving me the error Failed to build ContentView.swift. Failed to code sign ContentView.swift.

I have just created the project without any modifications to the auto generated code. Even the code signing has also been set properly. Note I'm using the Free Apple Developer Account. Could this cause any problem?

Following is the code from ContentView.swift file:

import SwiftUI

struct ContentView : View {
    var body: some View {
        Text("Hello World")
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif
Ash_B
  • 393
  • 1
  • 3
  • 8
  • 1
    While I'm not sure what exactly is going on, please note Xcode 11 is a beta and there are likely weird bugs that aren't yet fixed. – Burgan Jun 04 '19 at 17:38

10 Answers10

41

First select the Xcode using following command

sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

Then run

sudo xcodebuild -license

Hope it is helpful

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
tsp
  • 1,938
  • 18
  • 15
  • 8
    I tried the same on the mac terminal. On terminal first run **sudo xcode-select -s /Applications/Xcode.app/Contents/Developer** and then run **sudo xcodebuild -license**. Next you'll be prompted with license agreement, keep pressing '*space bar*' until reaching the end of the agreement and then type '*agree*'. Preview is working properly now. Thanks!! – Ash_B Jun 04 '19 at 18:18
  • 1
    Where did you find that ? I mean any doc or any link – Prashant Tukadiya Jun 08 '19 at 09:41
  • I followed the method suggested by @tsp, but got error which was solved using ‘sudo xcode-select -s /Applications/Xcode.app/Contents/Developer’ – Ash_B Jun 12 '19 at 15:35
  • 2
    For xcode beta, use sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer – Vinoth Vino Jul 22 '19 at 05:21
  • 1
    If you are using the preview release of Xcode 11 be sure to change the target of `xcode-select` to wherever `Xcode-beta.app` is located on your machine. – Jeevan Takhar Sep 06 '19 at 10:52
  • 1
    For some unknown reason, this fix doesn't work when the project name is swiftui. After I renamed the project, it started working – harshith__ Jun 17 '20 at 18:43
  • 3
    Not working here. Xcode 13.2.1, new project, getting 'Failed to build'. Nothing is working. – bauerMusic Jan 14 '22 at 17:30
  • 1
    Xcode 13.3, not working. – jalone Mar 23 '22 at 23:51
24

I solved that problem by going to the Xcode - Preferences -> Locations and assigning the Command Line Tools to Xcode 11. Also keep in mind that it is a beta, so it is unstable.

Diego Isco
  • 499
  • 3
  • 12
3

I had this problem as I was using // to comment beside my curly brackets to check I had them all in the right spot in my screen code. Xcode seems to cope with a few but once the screen gets too large and too many comments...... Now I comment on the line below and no problems!

3

Xcode SwiftUI Previews may occasionally fail with an error message about a modified .h file, such as “file Header.h has been modified since the module file Module.pcm was built: mtime changed.” (85938686)

Workaround: Delete the Clang module cache by running the following command in Terminal:

rm -rf "$TMPDIR/../C/clang/ModuleCache"

Then try to preview the file again.

https://developer.apple.com/documentation/Xcode-Release-Notes/xcode-13_3-release-notes

Murphy
  • 849
  • 7
  • 6
1

I had this problem too. I solved it by moving the file location of the file I was trying to preview. When the file was created it was down below the resources folder, so I moved it up and then preview worked.

0

I am currently having this problem with XCode 12.2, it says "Failed to build ContentView.swift." and when you click on the information's button it says "Extra tokens at end of #SourceLocation". Apparetly that was because i wrote a "// Comment" in the same line, next to a variable definition, so i solved this only moving the comment to the next line. I don't know who i can help this, but just wanted to share it. Regards!

0

Change your iOS version to the latest one. It will solve your issue. enter image description here

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
0

Renaming main project solved same issue on my Mac.

  • Rename the blue file in your project navigation.
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 29 '22 at 11:37
0

Just use Latin English Characters for your project. I tried everything above. Then, I deleted the project and recreate using just English characters.

alp tuğan
  • 26
  • 5
-1

I encountered this problem too. I download Xcode beta-1 before I download macOS 10.15. Then after my system upgrade to 10.15, the preview function didn't work. It turns out that Apple has released Xcode beta-2 version recently, so I downloaded and it worked. So, KEEP THE NEWEST VERSION OF XCODE!!!!

Evian
  • 1,035
  • 1
  • 9
  • 22