Listing 7.10 error message

Hello,

when i type the code from listing 7.10 like its in the book:

print(“aAcute: (aAcute.characters.count);
aAcutePrecomposed: (aAcutePrecompose.characters.count)”)

i got the following error messages
for the first line:

  • Expected “,” separator
  • Unterminated string literal

and for the second:

  • Invalid character in source file
  • Expected “,” separtor
  • Unterminated string literal
  • Expected expression in list of espressions

after changing the code as followed, the errors are gone.

print(“aAcute: (aAcute.characters.count); aAcutePrecomposed: (aAcutePrecompose.characters.count)”)

can someone please explain me why the Listing in the book raise the errors?

Thanks,
Chris

Please be careful when you write code and avoid to judge quickly. You have forgotten \ . Look again the book.
Also after changing the code as followed, you have forgotten again \ . The print you get is not that the book asks.

I have not forgotten the “”. It just disappeared after the copy paste to the forum.
To rule out that my problem is a typo i opened the solutions file for this chapter and changed the line brake to be the same as in the book. The result is the same error.

When the book has ; means it just separates aAcute: \ (aAcute.characters.count) from aAcutePrecomposed: \ (aAcutePrecompose.characters.count). It could have a comma instead.There is not enough space in the page for that here and so there are 2 lines. You can see that because in 1st line it doesn’t close print function with ").
If you wanted a second print to the below line you should use 2nd print function like that:
print(“aAcute: \ (aAcute.characters.count)”)
print(“aAcutePrecomposed: \ (aAcutePrecomposed.characters.count)”)
If you also hit enter making 2 lines after ; than aAcute.characters.count for example or parentheses change colour. They are all black. You should think now you are not writing code.
It’s not a typo of the book. There is not just space. Whenever you see something like that for print in the book write it in 1 line. Example page 116 -> Here you have to see that it’s 1 line for the print function

Last:
semicolons are required if you want to write multiple separate statements on a single line but try to avoid it. It’s not clean writing code. Example:

let cat = “:cat:”; print(cat)

I hope I helped now

thanks for the explanation.

Just to be sure i got it…

this:
print("aAcute: \(aAcute.characters.count), aAcutePrecomposed: \ (aAcutePrecompose.characters.count)")
works because xcode knows (because of the “”) that the code will continue on the next line.

and this not:
print("aAcute: \(aAcute.characters.count), aAcutePrecomposed: \(aAcutePrecompose.characters.count)")
even though the “” belongs to the “(aAcutePrecompose.characters.count)”

Essentially, yes…but I wouldn’t recommend taking advantage of that in your code.

If you really want that line break, then consider splitting the one long string into two strings:

print("aAcute: \(aAcuate.characters.count), " +
"aAcutePrecomposed: \(aAcutePrecompose.characters.count)")

…but note that the space before the plus (+) is necessary here.

(aAcutePrecomposed can go on the first line as you had it, but it may make more sense to place it on the same line as its value, so it’s easier to see at a glance that you’re just displaying labels and their values.)

I have a completely different explanation than Dim.

I think your issue revolves around the difference between a soft carriage return and a hard carriage return to the program that is automatically formatting your screen.

A hard carriage return always forces a new line, but a soft carriage return could be moved by a text formatter to a different location.

The line of code that caused you the problem was pretty long. If the source editor window was not wide enough to display the entire line, it would insert a soft carriage return in a place it chooses and wrap the line into the next line. It will choose a place that makes sense semantically (i.e. not in the middle of a word or a variable name, etc.)

When you looked at the listing in the book, there is nothing to indicate whether the first line of the print statement has a soft carriage return or a hard carriage return. I am betting that you inserted a hard carriage return after the semicolon, but in the actual code the book was showing, I am betting there was a soft carriage return that was inserted by the code formatter. A soft carriage return affects the display formatting only and is not actually in the source code. In this case, a hard carriage return is syntactically incorrect after the semicolon, and that was the source of the error.

You can see the difference between soft and hard carriage returns for yourself by manipulating the width of your source code window. As you make the window wider hard carriage returns will not be reformatted, however, soft carriage returns will be reformatted.

If you have Microsoft Word, you can clearly see the effect of hard and soft carriage returns by enabling “Paragraph Marks” and then changing the margins in the document.