【问题标题】:Accepting a callback to a function as param接受函数的回调作为参数
【发布时间】:2016-02-10 08:13:11
【问题描述】:

我正在使用 SnapKit,我试图将闭包作为参数传递给我的函数,该函数调用如下所示的 snp_makeConstaints,

func drawView(view: UIView, callback: (() -> Void)!) {
    self.addSubview(view)
    view.snp_makeConstraints(closure: callback)
}

我收到以下错误,

../example.swift:57:43: Cannot convert value of type '(() -> Void)!' to expected argument type '@noescape (make: ConstraintMaker) -> Void'

因此我添加了(make: ConstaintMaker) -> Void

导致以下错误, Use of undeclared type 'ConstraintMaker'

我不明白@noescape 是什么以及为什么说ConstraintMaker 未声明。

【问题讨论】:

  • 因为 snp_makeConstraints 期望 (make: ConstraintMaker) -> Void 而你试图通过 () -> Void?
  • @sliwinski.lukas 当我添加 (make: ConstraintMaker) 时,它显示以下错误,Use of undeclared type 'ConstraintMaker'
  • 你必须导入一些模块。检查 SnapKit 文档是哪一个。
  • 我已经包含了模块,我也可以直接使用它,但不能作为回调传递给我的方法。
  • @sliwinski.lukas 糟糕,我已将模块包含在主文件中,但未包含在我编写函数的文件中。

标签: ios swift snapkit


【解决方案1】:

在您遇到问题的 swift 文件顶部添加import SnapKit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-11
    • 2010-11-05
    • 2022-01-09
    • 1970-01-01
    • 2021-01-23
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多