Closure : Use unresolved identifier 'self'

swift

Solution

Looks very much like a bug (or at least, a behaviour of function-local structs/classes I can’t find documented). This works fine:

struct Foo {
    let someVal = 5
    lazy var someLazy: String = {
        return toString(self.someVal)
    }()
}

var foo = Foo()
foo.someLazy // string "5"

But this doesn’t:

func outer() {
    struct Foo {
        let someVal = 5
        lazy var someLazy: String = {
            // error: use of unresolved identifier 'self'
            return toString(self.someVal)
        }()
    }

    var foo = Foo()
    foo.someLazy
}

An inner struct inside an outer struct works, though:

struct Outer {
    struct Foo {
        let someVal = 5
        lazy var someLazy: String = {
            return toString(self.someVal)
        }()
    }

    var foo = Foo()
}

var outer = Outer()
outer.foo.someLazy

As @JeremyP says, you should file a radar.

Problem

I'm parsing the Swift Language Guide tutorial (from Apple iOS dev library) and for every chapter I create a separate swift file. In each file I create multiple functions where I isolate snippets of code that they provide. Everything worked on until testing the Strong Reference Cycles for Closures. For some reason if the class that contains a closure (for a computed property) is declared inside a function, then the closure cannot see the "self" reference of the enclosing class. Any ideas why ? It works fine if the class is not declared inside a function. ``` func strongRefClosure() { class HTMLElement { let name: String let text: String? lazy var asHTML: () -> String = { if let text = self.text { return "<\(self.name)>\(text)</\(self.name)>" } else { return "<\(self.name) />" } } init(name: String, text: String? = nil) { self.name = name self.text = text } deinit { println("\(name) is being deinitialized") } } var paragraph: HTMLElement? = HTMLElement(name: "p", text: "hello, world") println(paragraph!.asHTML()) } ```

Original source