0

I was trying to declare a closure expression in a class and have the following compile error: 'unowned' may only be applied to class and class-bound protocol types...

let id: String
var something: () -> String = { [unowned self] in
    return "hello" + self.id
}

Though, the following works (with lazy):

let id: String
lazy var something: () -> String = { [unowned self] in
    return "hello" + self.id
}

Why?

xtan
  • 19
  • 3
  • The problem is unrelated to closures or unowned references: You cannot refer to `self` in the initialization of a (non lazy) property. Essentially a duplicate of https://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other or https://stackoverflow.com/questions/24876985/class-type-does-not-have-a-member-named-variable-error-just-a-lack-of-class. – Martin R Jul 25 '17 at 08:30
  • Right. Got it! Thanks @MartinR – xtan Jul 25 '17 at 08:45

0 Answers0