Ch. 3, p. 43 ERROR: Use of unresolved identifier 'self'

I am getting error around using the self operator when running code from Chapter 3, p. 43. I THINK I have entered the code properly, but I am unsure if I am hitting another Swift 2.x - Swift 3.0 issue…?

//: Playground - noun: a place where people can play

import Cocoa

// METHODS allow you to add functionaliy to your data
// types. You can add methods to structures as well as classes
// and enums.
//
// INSTANCE METHODS operate within the context of a single
// instance of the type.
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: self.x + vector.x, // ERROR: Use of unresolved identifier 'self'
                   y: self.y + vector.y) // ERROR: Use of unresolved identifier 'self'
}

let gravity=Vector(x:0.0, y: 9.8)
gravity.x
gravity.y

Use of self in a function implies that the function is a method and therefore it must be part of a struct, enum, or class type.

struct Foo {
   ...
   let jibber:Jibber
   ...
   func jibberredBar () -> Bar {
      return Bar (jibber:self.jibber)
   }
}

In your code, put the function vectorByAddingVector inside the struct Vector.

I figured that out, but have no idea how to ‘close out’ a question if I figure it out subsequently.

Can that be done on this system?

Regards,

Steve O’Sullivan
650.464.3149

After BNR has started using this new platform, the delete-post button has become harder to find. You can, however, always edit and update your recent posts.