1
import Cocoa

public class Ut {
    public func foo(m: Int) -> Int {
        return m*m
    }
}

class ViewController: NSViewController {

    let j = 3
    let k = Ut.foo(j) // 'ViewController.Type' does not have a member named 'j'

...
Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
Gary M
  • 11
  • 2
  • Possible duplicates: http://stackoverflow.com/questions/25582853/type-does-not-have-a-member, http://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other, http://stackoverflow.com/questions/25855137/viewcontrol-type-does-not-have-a-member-named. – Martin R Apr 03 '15 at 15:22

2 Answers2

2

Unfortunately, you can’t access other properties when giving properties their initial values:

struct S {
    let a = 1
    // error: S.Type does not have a member named a
    let b = a + 1
}

Instead, you have to initialize these values inside init:

struct S {
    let a: Int
    let b: Int

    init() {
        // note, a must be initialized in here
        // too if b relies on it
        a = 1
        b = a + 1
    }
}

(also, it looks like you’re using Ut.foo as a class-level function but it’s a member function - but this particular error is about the property init)

Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
0

You have to make your foo(m: Int) as class function in order to call directly like that. Otherwise, you have to create an instance of Ut then call foo() on to this instance

class func foo(m: Int) -> Int {
        return m*m
}

then in other place: let k = Ut.foo(j)

If you pass j as parameter, this call must be placed inside a function, not at class level. If you want to call at class level, pass a value (like: let k = Ut.foo(5) )

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • Thanks much. In hindsight, I see the question was pretty ignorant because you have to instantiate a class, or somehow make it static, before using it. – Gary M Apr 05 '15 at 05:48