简体   繁体   English

Swift 2.0,closure = function()

[英]Swift 2.0, closure = function()

Hey I have a question that I cant seem to get it work 嘿,我有一个问题似乎无法让它发挥作用

I have closure type 我有封闭类型

public typealias VoidCompletionHandler = ()->Void

Then I create a optional variable 然后我创建一个可选变量

var favouritePropertyStateHandler:VoidCompletionHandler?

Then I call it 然后我打电话给它

self.favouriteCellStateHandler?()

All works good 一切都很好

self.propertyModel?.favouritePropertyStateHandler = { self.favouriteStateChanged() }

Woks perfectly with fucntion type Woks与fucntion类型完美搭配

func favouriteStateChanged()->Void

But why cant I just 但为什么我不能

self.propertyModel?.favouritePropertyStateHandler = self.favouriteStateChanged()

Types match - both are ? 类型匹配 - 两者都是?

()->Void  

The error I get is 我得到的错误是

Cannot assign a value of type 'Void' ('aka '()') to a value of type 'VoidCopletionHanlder?'

Solved 解决了

self.propertyModel?.favouritePropertyStateHandler = self.favouriteStateChanged

However that creates another problem, how do I not cause eternal retain cycle? 然而,这又产生了另一个问题,我怎么不造成永恒的保留周期? if I want self to be weak? 如果我想让自己变弱?

You're assigning the result from self.favouriteStateChanged() , not the function itself. 你是从self.favouriteStateChanged()分配结果,而不是函数本身。 Try 尝试

self.propertyModel?.favouritePropertyStateHandler = self.favouriteStateChanged

instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM