Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode - Invalid character in source file (Replace " " with " ")

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

Error

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

like image 903
Godlike Avatar asked Jan 28 '16 08:01

Godlike


2 Answers

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

like image 135
smileBot Avatar answered Sep 18 '22 15:09

smileBot


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.

like image 24
Strinder Avatar answered Sep 18 '22 15:09

Strinder