Xcode - Invalid character in source file (Replace “ ” with “ ”)

邮差的信 提交于 2019-12-30 05:53:30

问题


I have a question about Xcode (Swift). Sometimes when I write an if statement. Something like this:

if(a == 2){
//do Sth
} else {
//do Sth
}

Xcode throws me an error: "Invalid character in source file" I know it is no big deal and fixed by clicking on "Fix-it" but I want to know what is the problem. That doesn't make sense to me...

My Xcode is on 7.2 and I am using Swift - I don't know if this is just a Swift problem.


回答1:


I was having this problem. The way I solved it is by 1) copying whatever you have into Sublime Text or another code editor and having a look (you may need to enable viewing hidden characters). Mine had a code in there DC2 which is some sort of ASCII control character. For me it was caused by the fact that I had put the playground into manual execute and bound a keyboard shortcut to it that seemed to be entering that control character every time I executed it using the shortcut. I just changed the shortcut and removed the ASCII characters as I was able to see them in Sublime Text. (The link in the comments to the OP concerning left to right mode made no sense and didn't work for me.)




回答2:


For me it's the ESC key - it's completely reproducible! If I go to search and hit ESC in the search window I also find the occurrences marked - and can replace those with nothing. But it's not shown even with "Show invisible"!

final solution: Bind ESC keybinding to "Cancel". It's that simple.




回答3:


I encountered this problem when I was using a non-Apple keyboard with my iMac. Probably your case too. They add unnecessary control characters! One way to fix it in Xcode ischoose from the menu Editor>Show Invisibles and see if there are any unwanted characters where the error is being thrown and remove them.




回答4:


I'm having the same issue. I found a solution after extensive testing. Hopefully it could save others extensive hours of researching into this terrible Xcode experience.

The problem is I have a FILCO Minila keyboard, which has the 'App' key right next to 'left arrow'. I would accidentally hit 'App' when typing really fast, and it enters a 'DEL'(Data Link Escape, Unicode 0010) invisible character. It could only be seen when copy-pasted to Sublime. It's not even visible under Xcode's own 'Editor -> Show Invisibles'(Talking about good user experience...)

Magically this doesn't happen to Sublime or VSCode, only to Xcode. So no excuses for Xcode I guess?

The fix is to download Karabiner, an advanced keymapper for macOS, and invalidate the 'App' key or map it to something else. Karabiner would also invalidate your keymapping in the system default keyboard config. So you would have to remap stuff like 'cmd', 'alt' if you've customized them. If so, be sure to map both left & right modifier keys in Karabiner, because according to Karabiner's key event viewer, the system default keymap would map left_alt & left_cmd to right_cmd and right_alt... Just annoying as hell...

It took me 2 months to realize this problem and 2 hours to actually find out it's the 'app' key that's the culprit and fix it.




回答5:


Editor->Show Invisibles did not show the character that was causing a problem for me. No-break-space (u00a0) will be shown as a space with no "dot" indicator. However, that's really hard to see and I completely missed it the first time. Zoom helps.

One way I found to detect invalid characters is to use Visual Studio Code and enable the settings:

"editor.renderControlCharacters": true "editor.renderWhitespace": "all"

This has the same visibility issue with no-break-space as Xcode, however it does show other special characters.

To make it more obvious and possibly detect more invalid characters, install the extension "ascii-unicode-escape" and follow the instructions.

I don't understand why Xcode allows these invalid characters, inserts them into your source files for no reason, and doesn't even give an option to detect them! I'm trying to write code, not secrets in invisible ink, damnit!! I guess it's less of an issue for compiled code, which will show an error, but not so convenient with shader code.




回答6:


I'm using XCode 10.1.

With Xcode: Xcode does not help even after showing invisibles.

Then you can copy the code to Sublime Text => you will see these invisible characters which are the <0x0f>. Click the image to see in details.

With Sublime Text 3, it works!

=> Then you know where to remove the redundancies. Hope that helps!




回答7:


Alt+Space command creates a special hidden character in Xcode files. You can see details of bug here

To find that special character I've used kinda childish workout (Apple might need to fix this properly as 'show invisibles' doesn't actually show that character.)

Search for 'Space' characters in file and then check for hidden like in screenshot.

See below screenshots, through which you can guess the hidden special character's location in file.




回答8:


Try reverting all Xcode settings to their original state using

defaults delete com.apple.dt.Xcode

in the Terminal



来源:https://stackoverflow.com/questions/35056391/xcode-invalid-character-in-source-file-replace-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!