Yes, Swift only supports UTF-8 input (in any environment, not just Xcode). That assumption's woven through the compiler in a few ways, mainly assuming that some content can be copied directly from the source file into output, but supporting UTF-16 in particular would be tricky because of the 16-bit code units, which would require reworking the lexer quite a bit, which could affect parsing speed (but might not be significant).
Do you have a particular reason why it'd be useful to accept source files encoded as UTF-16?
I have one ancillary question: what is the purpose of the Xcode > Preferences > Text Editing > Default Text Encoding if using anything but UTF-8 may cause a problem?
Yeah, it'd be worth filing a bug against Xcode and/or Swift to make sure the error message is good. I suspect some of the null bytes from the source file are making it into the output unescaped somehow (UTF-16-encoded ASCII characters have all zeroes in the high byte).
@Avi Yes, I agree that some compilers accept standards other than Unicode or different Unicode encodings. Have you ever seen Xcode’s Preferences → Text Editing → Default Text Encoding setting actually be respected when creating a new file? I tested it with Swift, Objective-C, and C projects, and in every case the new files were created as UTF-8, even when my Default Text Encoding was set to UTF-16.
In my experience with Xcode 26.1, the Text Encoding and Line Endings features are broken. The File Inspector's Text Encoding field does not detect a file’s actual encoding, and the conversion dialog asks for confirmation but produces no changes — Git shows no diffs afterward. The Xcode settings related to these features also appear to be non-functional. I have set UTF-16 as default encoding but new files are always UTF-8. I posted a thread about this issue on the Apple Developer Forums but have not received any response. For detecting and converting file encodings or line endings, I currently use Visual Studio Code.
The Xcode editor, if a file is encoded in UTF-16 shows an error, project does not compiles and shows an error
input files must be encoded as UTF-8 instead of UTF-16