swiftui extra tokens at the end of #sourceLocation directive

My SwftUI code suddenly started getting this error but only happens when using Canvas. Works fine in the Simulator and no errors or warnings. Any idea if this is a bug in SwiftUI or something in my code? I have looked at all of my # statements and they all seem to have matching # statments. I have also looked at the sorce code files in hex and they all seem to be ended properly with a 0A. sooo... don't know how else to continue.

Replies

Could you post the part of code with the directive ?


You could try to

- copy the complete line as well as the next ones and check in a text editor for hidden characters

- remove completely the statement and type it again.

Hello, I know one cause of this error and I am assuming it is a bug in xcode. If you have comments on the same line as an opening bracket of a closure or an if statement or anything of that nature it will throw this error. As far as I know comments should have no effect on your code which is why I am pretty sure this is a bug but if you still have this issue check the lines with opening brackets for comments and delete them that should fix it!


Hope this helps


Current XCode Version: 11.4.1

  • Yeah, comment by espidle worked fine for me! He is mostly right!

  • This answer helped me. Thx 🙂 The error appears even if you leave a comment on the same line with var yourCustomView: some View { // here

Add a Comment
I had the same issue on Xcode 11.5.

As @espidle mentioned, removing any comments from the lines where there are open bracket seems to solve this for me.
Recently got this problem. Solution from above helped me. Just delete all comments near opening brackets. My question is, when Apple will fix this problem ?
The exact same problem happened to me too. I solved it by moving those comments outside the curly brackets. That's all.

I had this same problem June 28 2021 with xCode 12.5. I like to add a short descriptive comment at the start of a func() { But apparently it causes errors if the comment is not on a new line. The resulting error messages are very confusing. It cost me a few hours of misguided "fixes" and restoring from backups, before finding this forum post and explanation. Thanks for posting!

Apple is overdue to fix this.