Xcode - 源文件中的无效字符(将“”替换为“”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35056391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode - Invalid character in source file (Replace " " with " ")
提问by Godlike
I have a question about Xcode (Swift). Sometimes when I write an if statement. Something like this:
我有一个关于 Xcode (Swift) 的问题。有时当我写一个 if 语句时。像这样的东西:
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...
Xcode 向我抛出一个错误:“源文件中的字符无效”我知道这没什么大不了的,可以通过单击“修复”来修复,但我想知道问题出在哪里。这对我来说没有意义...
My Xcode is on 7.2 and I am using Swift - I don't know if this is just a Swift problem.
我的 Xcode 是 7.2 并且我使用的是 Swift - 我不知道这是否只是一个 Swift 问题。
回答by smileBot
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.)
我遇到了这个问题。我解决它的方法是 1) 将您拥有的任何内容复制到 Sublime Text 或其他代码编辑器中并查看(您可能需要启用查看隐藏字符)。我的 DC2 中有一个代码,它是某种 ASCII 控制字符。对我来说,这是因为我已将操场置于手动执行状态,并为其绑定了一个键盘快捷键,每次我使用该快捷键执行时,该快捷键似乎都在输入该控制字符。我只是更改了快捷方式并删除了 ASCII 字符,因为我可以在 Sublime Text 中看到它们。(关于从左到右模式的 OP 评论中的链接毫无意义,对我不起作用。)
回答by Strinder
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"!
对我来说,它是 ESC 键——它是完全可重复的!如果我去搜索并在搜索窗口中点击 ESC,我也会找到标记的事件 - 并且可以用任何东西替换它们。但是即使使用“Show invisible”也不会显示!
final solution: Bind ESC keybinding to "Cancel". It's that simple.
最终解决方案:将 ESC 键绑定绑定到“取消”。就这么简单。
回答by hohohmm
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.
我有同样的问题。经过大量测试,我找到了解决方案。希望它可以为其他人节省大量时间来研究这种糟糕的 Xcode 体验。
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...)
问题是我有一个 FILCO Minila 键盘,它在“左箭头”旁边有“App”键。打字速度非常快时,我会不小心点击“应用程序”,它会输入一个“DEL”(数据链接转义,Unicode 0010)不可见字符。只有在复制粘贴到 Sublime 时才能看到它。它甚至在 Xcode 自己的“编辑器-> 显示隐形”下都不可见(谈论良好的用户体验......)
Magically this doesn't happen to Sublime or VSCode, only to Xcode. So no excuses for Xcode I guess?
神奇的是,这不会发生在 Sublime 或 VSCode 上,只会发生在 Xcode 上。所以我猜 Xcode 没有任何借口?
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...
修复方法是下载用于 macOS 的高级键盘映射器 Karabiner,并使“App”键无效或将其映射到其他东西。Karabiner 还会使您在系统默认键盘配置中的键盘映射无效。因此,如果您自定义了“cmd”、“alt”等内容,则必须重新映射它们。如果是这样,请务必在 Karabiner 中映射左右修饰键,因为根据 Karabiner 的按键事件查看器,系统默认键盘映射会将 left_alt 和 left_cmd 映射到 right_cmd 和 right_alt ......简直烦人......
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.
我花了 2 个月的时间才意识到这个问题,而我花了 2 个小时才真正找出罪魁祸首是“应用程序”密钥并修复它。
回答by Krishhna
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.
我在 iMac 上使用非 Apple 键盘时遇到了这个问题。可能你的情况也是如此。他们添加了不必要的控制字符!在 Xcode 中修复它的一种方法是从菜单 Editor>Show Invisibles 中选择,看看是否有任何不需要的字符抛出错误并删除它们。
回答by Steve
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.
Editor->Show Invisibles 没有显示给我造成问题的角色。不间断空格 (u00a0) 将显示为没有“点”指示符的空格。然而,这真的很难看到,我第一次完全错过了。缩放有帮助。
One way I found to detect invalid characters is to use Visual Studio Code and enable the settings:
我发现检测无效字符的一种方法是使用 Visual Studio Code 并启用设置:
"editor.renderControlCharacters": true "editor.renderWhitespace": "all"
“editor.renderControlCharacters”:真“editor.renderWhitespace”:“全部”
This has the same visibility issue with no-break-space as Xcode, however it does show other special characters.
这与 Xcode 具有相同的无中断空间可见性问题,但它确实显示了其他特殊字符。
To make it more obvious and possibly detect more invalid characters, install the extension "ascii-unicode-escape" and follow the instructions.
为了使其更明显并可能检测到更多无效字符,请安装扩展“ascii-unicode-escape”并按照说明进行操作。
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.
我不明白为什么 Xcode 允许这些无效字符,无缘无故地将它们插入到您的源文件中,甚至没有提供检测它们的选项!我正在尝试编写代码,而不是用隐形墨水写秘密,该死的!!我想这对于编译后的代码来说不是问题,它会显示错误,但对于着色器代码就不太方便了。
回答by nickypub
I'm using XCode 10.1.
我正在使用 XCode 10.1。
With Xcode: Xcode does not help even after showing invisibles.
使用 Xcode:即使显示不可见,Xcode 也无济于事。
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.
然后你可以将代码复制到 Sublime Text => 你会看到这些不可见的字符,它们是<0x0f>。点击图片查看详情。
With Sublime Text 3, it works!
使用 Sublime Text 3,它可以工作!
=> Then you know where to remove the redundancies. Hope that helps!
=> 然后你知道在哪里删除冗余。希望有帮助!
回答by Monish
Try reverting all Xcode settings to their original state using
尝试使用以下命令将所有 Xcode 设置恢复为原始状态
defaults delete com.apple.dt.Xcode
in the Terminal
在终端
回答by s.zainulabideen
Alt+Spacecommand creates a special hidden character in Xcode files. You can see details of bug here
Alt+Space命令在 Xcode 文件中创建一个特殊的隐藏字符。您可以在此处查看错误的详细信息
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.)
为了找到那个特殊的角色,我使用了有点幼稚的训练(Apple 可能需要正确解决这个问题,因为“show invisibles”实际上并没有显示那个角色。)
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.
看下面的截图,通过它你可以猜测隐藏的特殊字符在文件中的位置。