Trouble copy/pasting code from book

I’m having trouble copy/pasting bits of code from the book (Please don’t tell me it’s best to learn by typing all the examples in :). The most common, but least troublesome, is the formatting ends up all weird and I have to manually fix the placement of braces, etc. This is easy enough so it’s not really a big deal. What’s more troublesome is that, I think, some extra information or hidden characters from the PDF are being copied into the source file as well when I copy/paste code from the book.

For example, in the very first bit of code we’re asked to put into our project in chapter 1 on page 37, if I copy the code into GameScene.swift, it seems to copy fine, but somehow maybe some hidden characters from the PDF are being pasted as well because if I position the cursor on line 2 and hit enter to add a newline at the beginning of the class definition and move it down a line, it actually replaces the entire line with the one below it in XCode 7!

Please see the two screenshots at below, the first is the code after I’ve copied it into my project, the second is the result of placing the cursor at the beginning of line 2 and hitting enter.


After hitting return to add a new line

Does anyone know why this is happening? It’s definitely not a problem with XCode because when I type the code in manually I can add a newline fine. It’s a result of copying it from the PDF.

Long story short, is there a good way to copy the code examples from the book into XCode that preserves formatting and doesn’t introduce anything extra?


PS I’m using XCode 7.2.1, and the latest version of OS X Preview on El Capitan to read version 1.2 of the book.

Unfortunately, such issues happen to me as well on occasion when I try to copy/paste code from a PDF book. In those cases I usually paste the copied code into a text editor first, such as Sublime, clean it up, and then copy/paste that into my code editor. I know, it’s a pain, but…