Computed properties

I have been struggling with this computed properties for days now. I keep getting the error below and can’t figure out why.

expected declaration on the line var length: Double

[code]struct Vector {
var x: Double
var y: Double

init() {
    self.init(x: 0, y:0)
}
init(x: Double, y:Double){
    self.x = x
    self.y = y
}
func vectorByAddingVector(vector: Vector) -> Vector {
    return Vector(x: x + vector.x,
                  y: y + vector.y)
}

​v​a​r​ ​l​e​n​g​t​h​:​ ​D​o​u​b​l​e ​{
   ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​ ​s​q​r​t​(​x​*​x​ ​+​ ​y​*​y​)
​ ​ ​ ​}[/code]

I’d say stop cpoying code from an epub or so :wink:

You have weird unicode symbols in there it seems.

Just type the exact same thing a line above (+ the missing } at the end) and it should work.

The first hint at this is that Xcode doesn’t color the var keyword in your code.

It sucks big time that you cannot copy ‘normal’ text from the iBook version of the book for that reason.

Thanks for the help that did the trick. I swear I typed it the first time and when it didn’t work I tried copying. I probably had a typo the first time and then when I copied it I ended up with the unicode characters. :blush:

Well another important lesson learned. Check for unicode characters. How did you find the characters? I still don’t see them.

I didn’t see them either and I still don’t know which ones are the culprits, but when using your left and right cursors through the lines you can tell that there is something wrong immediately.

After reading tkrajacic’s hint, I pasted your code into macvim–an open source computer programming editor–and there were some weird vim codes appearing between each letter of the line flagged with the error:

<200b>v<200b>a<200b>r<200b>....

Then I used HexFiend–an open source hex editor–to look at the text. A hex editor will show you every byte. Some text editors, e.g. Xcode, will skip bytes that they don’t understand or include them but not display anything for that byte–not even a space. If I tell HexFiend to interpret the bytes as ASCII text, it looks like this:

  ​v ​a âr€‹ ...

If I tell the hex editor to interpret the bytes as UTF-8, it looks like this:

...​v​...a...r...

The dots mean that there are three bytes before the ‘v’, but that those bytes contain integers that are not valid UTF-8 character codes. The extra bytes before each character contain the same three integers. Here are the integers in hex format:

E2 80 8B

Some program somewhere reads those integers–probably for formatting, e.g. the codes could be an instruction to display/print the following character in bold. Was that code bolded in the text you copied? I’ve seen similar garbage when people write code in MS Word, then copy and paste the text into their program.