ChatGPT解决这个技术问题 Extra ChatGPT

转换为最新语法时出现错误“swiftc failed with exit code 1”

I was trying to convert syntax of old Swift project to new one as per few suggestion on Stack Overflow I tried converting it through Edit > Convert > To latest Syntax that did not work properly instead I started getting new errors shown as below

Command/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1

And

Command /usr/bin/ditto failed with exit code 1

https://i.stack.imgur.com/1x7SP.png

I tried Xcode 6.0.1 Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1, but it's a different situation.

All Googlers: This error is a "catch all" that can have many reasons. Cleaning often helps. If not, retrace your steps and find out which changes cause the compiler to fail. Then, open a bug report.
The other options is to build your code on the command line using 'xcodebuild', it some presents the errors in a more obvious way than the 'parsed' log output in Xcode. You'll then have better luck in finding the issue

A
Amit Singh

Have you tried to do clean with Clean build folder?

The combination keys is:

ALT + SHIFT + COMMAND + K


@ctpanchal, cleaning will not.
M
Manuel

Try delete Derived Data folder and Build it.

Go to Xcode -> Preferences -> Locations for get the derived data path


M
MRizwan33

Clean Xcode. (cmd+shift+k) Quit Xcode completely. Open project again. Wait for Xcode to indexing files. Now run your project.


you saved me bro literally
A
Alaa Awad

Just had the same issue. For me it was because I had renamed some of the project directories. I had a ton of red files in my project navigator. To solve, follow these steps:

click on the directory in project navigator where many files show as red Under "Identity and Type" in the File Inspector (on the right side), click Containing directory Select the new directories Rebuild - command shift K, command B


This was my case, I pull an empty git repo to copy my code to. After moving my project file to the new folder, I got this switch error. This answer fixed my problem, thanks!
Yep. after merging a re-write some bloke did. gotta click around and find the red (missing/moved) files and then delete the red. Derived data, clean, et al did not work.
C
Cœur

As far as I know there can be multiple reasons why a compiler fails. Although cmd+alt+shift+k will help most of the time. If this fails, then click on the error message and try to debug it.

For example in the image below, if you read the message, we get to know that, there has been a name duplication. Fixing the name duplication will fix the issue.

https://i.stack.imgur.com/rHwNQ.png


C
Cœur

Just remove inactive file(deleted file) from Build phases

then

Clean Xcode. (cmd+shift+k)


How did you remove the inactive file from build phases?
for me, this is what worked! I just went to see the Build phase and I realized that the file with the problem, was highlighted in a different color, and it had no path
G
Gowtham Sooryaraj

delete Derived Data folder and Build it.

Go to Xcode -> Preferences -> Locations for get the derived data path or cmd+Alt+shift+k

OR

Just remove inactive file(deleted file) from Build phases


i
ivoroto

Cleaning the project wasn't working for me, this is because I didn't pay attention to the message right above (Xcode highlights only the final command).

In my case it was

<unknown>:0: error: filename "FileDuplicated.swift" used twice: '/../filepath/FileDuplicated.swift' and '/../filepath/FileDuplicated.swift'
<unknown>:0: note: filenames are used to distinguish private declarations with the same name
Command /../bin/swiftc failed with exit code 1

I had to change the name of one of the two files and it was solved.

Hope this can help someone, cheers


Thanks @ivoroto, its really helped me. No one will notice this in the console logs.
Those two locations seem to be exactly the same
V
Vishnuvardhan

In my case it was due to the few swift files which has been showed grayed out in the Build Phases section. After removing those files it was successful.


g
garg

In my case I deleted few files from project and when try to commit the code, the files was showing there . To solve this issue. 1. Open terminal 2. Type git clean -n // it will show you list of files which are deleted from project but still are saved somewhere 3. Type git clean -f // All unwanted files will be removed


m
mohit

In my case, it was "Other swift flags". I had declared a flag there but not -D, so this was not getting recognized and resulted in the same error


M
MRizwan33

In my case swift development snapshot was selected instead of xcode 9.2. here are the steps and image.

xcode on screen and click on xcode top menu bar. Than go to toolchains option and check on xcode 9.2. thats it.

Note: If no toolchain found in your xcode than download from Here and install it. (after installation restart xcode). Happy Coding!!!


you should download the toolchain first and install it and restart your xcode toolchain can be found swift.org/download/#releases. if still have any problem u can ask.
Error isn't fixed after installing that toolchain package.
what kind of error its showing and have you installed .pkg toolchain file? and also restarted your xcode?. kindly provide you error details.
Yes i did install and got toolchain in Xcode. Same error retained
is toolchain visible in xcode? and is selected xcode version.. in toolchain? and have u applied again all methods above mention?
x
xdeleon

Ok, my turn now. In my case I had an existing project. I copied in a few files from another project. One of the files I brought in was SettingsViewController.swift and there was already a file by the same name I was not aware of. The files were in different directories of the project so no duplicate message was shown. The compile/link messages offered no clue to this. I figured out the issue by adding one file at a time to the project (there were 12 total) and the building each time until I found the offending file. I renamed the new file and project builds now.


L
Lucas van Dongen

In my case it was a compiler flag change that didn't work well. It took a few compiles before it actually started failing though! Xcode caches what it compiles.


K
Kevin Morris

I just had this issue. It's due to path references. Have you been moving files around and/or copying folders? Check if .xcodeproj is inside or outside of the main project folder. Also, make sure that the project's file and directory structure are intact.

/yourFolderProjectName/ <-- contains your .xcodeproj file AND folder with Xcode project name

/yourProjectFolder <-- here are .swift, info.plist, assets folder, etc.

/yourProjectXcodeFile.xcodeproj


C
Chris Garrett

Another thing to check:

In my case I had deleted a row in the "Other Swift Flags" section in the Build Settings screen under the Release setting.

That row had been a value for a compiler flag key/value pair. So the key was there but not the value.

Once I deleted the key as well, then the build worked again.


B
Balaji

This issue is happening because of pods

So Follow the below steps to resolve this issue

Remove all the pods and re-install again. Remove all the pods using below this command (rm -rf "pwd/Pods/")

Quit the Xcode and open it again and build the app with device (generic iOS device).

Now check this issue will be resolved.


N
Nupur Sharma

I had this error when Xcode found two .swift files with same name. Rename one of them and build again.


A
Akhilesh Mourya

Wait for completing the indexing and run the project again. You must get an error then check it which may be Objective-c bridging file not found error. This error comes if you are using any Objective-C library or code directly in the project and your project unable to find the bridging file in the system. This issue basically caused by, if project is unable to find any file in the system which is using in the project (path issue).


C
Cœur

I faced this issue while using the Swift auto-generated header file in my project (named as 'TargetName'-swift.h) to use Swift classes in Objective-C.

There was a typo in the file where I had imported this swift header. Correcting the typo helped me resolve the issue.


C
Cœur

Clean the build in Xcode cmd+shift+k. Update the pods. look the where errors showing. Add the missing files in Xcode. After Run the Code. Working fine.


P
Peter Walter

Try below if none of the above working for you.

My project location was desktop/my_project_folder

I tried all above then i made simple change.

I create one more folder on desktop past my project .

https://i.stack.imgur.com/vijHt.png

Cause of problem in my case:

This problem occurred when i change my project name in xcode & project folder name from desktop.


m
mfaani

I was working on a private pod and was also pointing to it locally. I had made some file name changes.

Hence I was getting this error.

All I had to do was to run pod install so it would reflect the file name changes. After that it compiled.


J
Janine Kroser

In my case I copied a core data entity and only renamed the entity but not the class. So go to your xcdatamodel and select the enitity > rename also the class name


J
Jonathan Kopp

Had the same issue but with duplicate file references. Deleting Derived Data and cleaning had no success. But I got it fixed by doing this:

Navigate to your project.pbxproj -> open with your text editor of choice(I use atom).

Command+F and find the duplicate file sources and then delete them.

Build/Run and should be fixed.


g
green_knight

For me, it was the keychainAccess problem as described in this answer (my error messages were completely opaque, all I had to go by was 'failed with exit code 1')