ios - 无法调用非函数类型 DateCallBack 的值

标签 ios swift3 xcode8

我正在编写的这段代码在迁移到 swift 3 之前曾经发挥过作用。但是,世界末日,我正在将它迁移到 XCode 8,而且我似乎无法解决代码中的这个错误。这个问题超出了我的理解范围。我想我已经错过了这里的基础知识。 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()
        }
    }
}

我得到的错误是两个函数 self.callBack 上的Cannot call a value of non-function type DateCallBack,这是两个函数的第二行。 请解释一下我也错过了什么。

最佳答案

您定义的函数定义为 (_ year: Int?, _ month: Int?) -> Void 因此调用时没有参数名称(因为 _ 在每个参数之前)。

所以你可以这样调用它:

self.callBack(yearIndex, monthIndex) 

如果你希望函数调用看起来像你的代码

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

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

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

关于ios - 无法调用非函数类型 DateCallBack 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40397343/

相关文章:

ios - 以编程方式创建对象并更改该对象的位置

ios - 以编程方式创建一个扩展的 UItableViewCell

ios - 如何使用 NSManagedObject 的新 fetchRequest 函数发出获取请求?

swift - 无法在 Swift 3 中创建范围

ios - 混合 Swift 和 Objective-c 项目的 Xcode 8 生成了 "ModuleName-Swift.h" header 未找到

ios - 无法在 Keychain Xcode 8 GM (Swift 3) 中存储数据

ios - 如何在 Apple 的 UITableView 样式中获取 subview 的框架位置?

ios - 如何使用 Meteor/Cordova iOS 应用修复 waitForSyncReply 超时?

swift - 从 CALayer 或 NSView 获取图像(swift 3)

Swift 3 UITableView 部分显示在列表后面