2016-11-03 30 views
0

我写的代码片段用于在迁移到swift之前工作魅力3.但是,世界末日,我将它迁移到XCode 8,我似乎无法解析这个错误在我的代码中。这个问题出于我的理解。我想我已经错过了这里的基础知识。 DateCallBack是我在声明我的类之前定义的一个typealias。无法调用非函数类型的值DateCallBack

typealias DateCallBack = ((_ year: Int?, _ month: Int?) -> Void) 

然后我宣布了一个DateCallBack的实例。

private var callBack: DateCallBack! 

然后是类的扩展中,我已经实现了两个方法

func cancelButtonAction() { 
    self.callBack(year: nil, month: nil) 
    self.animateOut { (completion) in 
     if completion { 
      self.removeFromSuperview() 
     } 
    } 
} 

func doneButtonAction(_ yearIndex: Int!, monthIndex: Int!) { 
    self.callBack(year: yearIndex, month: monthIndex) 
    self.animateOut { (completion) in 
     if completion { 
      self.removeFromSuperview() 
     } 
    } 
} 

而我得到的错误是Cannot call a value of non-function type DateCallBack两个功能self.callBack是两者的功能第二行。 请解释我也错过了什么。

+1

如果你像这样'self.callBack(yearIndex,monthIndex)''调用它,你会得到同样的错误吗? –

+0

谢谢你的队友,那是我无法弄清楚的问题。 – amagain

+0

不客气,我会将评论转换为答案,以便您接受它。 –

回答

1

您定义的函数定义为(_ year: Int?, _ month: Int?) -> Void所以没有参数名称时,这就是所谓的(因为_每个参数之前的)。

所以,你这样称呼它:

self.callBack(yearIndex, monthIndex) 

如果你想在函数调用看起来像在你的代码

self.callBack(year: yearIndex, month: monthIndex) 

你应该定义函数象下面这样:

(year: Int?, month: Int?) -> Void 
+0

精美的解释,我需要! – amagain

0

这解决了我的问题,谢谢@bogdanf

self.callBack(yearIndex, monthIndex) 
相关问题