Reputation: 339
I'm trying to create a closure to clear my cache, both of which are intended to be instance variables. When I try to invoke [unowned self], I get the error "'unowned may only be applied to class and class-bound protocol types, not (UIViewController) -> () -> UIViewController'"....I'm not sure why it is raising this. Is there no retain cycle created by invoking self in instance variables? If so, why? Thank you in advance, here is the concerned code snippet
class UIViewController
{
var repostCache : [String : Bool] = [String : Bool]()
let clearRepostCache = { [unowned self] in
self.repostCache = [String : Bool]()
}
}
Upvotes: 0
Views: 62
Reputation: 47886
The problem is that you cannot use self
until phase1 initialization finished. So, you cannot use self
for property's initial values.
You may need to move the code using self
somewhere into an instance method (or into an initializer after phase1 initialization).
For example:
class ViewController: UIViewController {
var repostCache: [String: Bool] = [:]
private(set) var clearRepostCache: (()->Void)!
override func viewDidLoad() {
clearRepostCache = { [unowned self] in
self.repostCache = [:]
}
}
}
You can find the documentation about Two-Phase Initialization here:
Class Inheritance and Initialization
And about the confusing diagnostic message, better send a bug report.
Upvotes: 2