# May Swift source code be in UTF-16 in XCode?

**URL:** <https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494>\
**Category:** Using Swift\
**Tags:** xcode, utf16\
**Created:** [March 12, 2020, 2:45pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494 "2020-03-12T14:45:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![PatrickA](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@PatrickA](https://forums.swift.org/u/PatrickA)\
**Post date:** [March 12, 2020, 2:45pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/1 "2020-03-12T14:45:30Z")

</div>

I am able to compile in Swift a very simple source file encoded in UTF-8 (with or without BOM).

But when I try to build the same source file but now encoded in UTF-16, I get an error : "Invalid Swift parseable output (malformed JSON)".

The file (main.swift) :

```
import Foundation
let str : String = "abc"
print (str)

```

What is happening ? What JSON is malformed?

Note that I can see the UTF-16 file in the code editor, I can edit it, but I cannot build it.

In Xcode \> Preferences \> Text Editing, I set "Default Text Encoding" to UTF-16BE/LE but to no avail.

Any help is appreciated, does Swift support only UTF-8 source files?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 12, 2020, 7:18pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/2 "2020-03-12T19:18:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![PatrickA](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@PatrickA](https://forums.swift.org/u/PatrickA)\
**Post date:** [March 12, 2020, 7:29pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/3 "2020-03-12T19:29:43Z")

</div>

Thank you, Jordan.

No, I don't have a pressing need to accept source files encoded as UTF-16, I was just testing how XCode would behave under various circumstances.

---

<div class="post-metadata">

**Author:** ![PatrickA](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@PatrickA](https://forums.swift.org/u/PatrickA)\
**Post date:** [March 12, 2020, 8:30pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/4 "2020-03-12T20:30:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [March 12, 2020, 8:53pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/5 "2020-03-12T20:53:21Z")

</div>

> [@PatrickA](#):
>
> 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?

Xcode supports languages other than Swift.

---

<div class="post-metadata">

**Author:** ![michelf](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michelf/32/1180_2.png) [@michelf](https://forums.swift.org/u/michelf)\
**Post date:** [March 12, 2020, 9:03pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/6 "2020-03-12T21:03:01Z")

</div>

> [@PatrickA](#):
>
> "Invalid Swift parseable output (malformed JSON)"

That sounds rather off the mark as an error message. I wonder where it comes from.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 12, 2020, 9:22pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/7 "2020-03-12T21:22:24Z")

</div>

Moreover, you can edit files that are not source code from within Xcode.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 12, 2020, 9:26pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/8 "2020-03-12T21:26:08Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![blazej.sleboda](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@blazej.sleboda](https://forums.swift.org/u/blazej.sleboda)\
**Post date:** [December 8, 2025, 12:04pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/9 "2025-12-08T12:04:37Z")

</div>

When compiling a file encoded in UTF-16, the Swift 6.2.1 compiler fails with the following error:

> input files must be encoded as UTF-8 instead of UTF-16

 ![swift compiler, error input files must be encoded as UTF-8 instead of UTF-16](https://global.discourse-cdn.com/swift/original/3X/1/b/1b94fd031cef41e81291045997adc531ae1d5156.png)

---

<div class="post-metadata">

**Author:** ![blazej.sleboda](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@blazej.sleboda](https://forums.swift.org/u/blazej.sleboda)\
**Post date:** [December 8, 2025, 12:12pm UTC](https://forums.swift.org/t/may-swift-source-code-be-in-utf-16-in-xcode/34494/10 "2025-12-08T12:12:42Z")

</div>

@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](https://developer.apple.com/forums/thread/806792) 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

 ![Xcode error input files must be encoded as UTF-8 instead of UTF-16](https://global.discourse-cdn.com/swift/original/3X/3/e/3e4f806f3fc43da0506ca5b1a7fb76e400c9d659.png)
