swift - 在 Swift 中将成员函数分配给成员变量

标签 swift function functional-programming

是否可能,或者该函数是否需要声明为staticclass

class SimManager {
    var particleSimulation: (inout Particle, Float) -> Void = updateParticles

    func updateParticles(inout p: Particle, deltaT: Float) -> Void {
       ...
    }
}

给出编译器错误:

SimManager.swift:36:63: Cannot convert value of type 'SimManager -> (inout Particle, deltaT: Float) -> Void' to specified type '(inout Particle, Float) -> Void'

最佳答案

articleSimulation的初始化移至类的指定初始值设定项。

class SimManager {
  var particleSimulation: ((inout Particle, Float) -> Void)!

  func updateParticles(inout p: Particle, deltaT: Float) -> Void {
  }

  init() {
    self.particleSimulation = updateParticles
  }
}

关于swift - 在 Swift 中将成员函数分配给成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35761465/

相关文章:

r - 编写一个可跟踪的 R 函数,模仿 LAPACK 的 dgetrf 进行 LU 分解

ios - 以编程方式添加时,rightAnchor 约束不适用 - swift

ios - requestLocation 没有调用 didUpdateLocations

python - 如何将附加参数(除了参数)传递给函数?

PHP 函数 'return' 未返回

functional-programming - 填充二进制 block 惰性序列

algorithm - Clojure 奎尔 : nested loops while drawing lines

haskell - 结构化数据验证的依赖类型

ios - 增加最后一节和新标题之间的高度

ios - SpriteKit 中的更新功能与 iPhone X 不同