【问题标题】:Is KVC possible with Swift?Swift 可以实现 KVC 吗?
【发布时间】:2016-05-08 17:36:56
【问题描述】:

我正在尝试在 Swift 中创建与以下 Objective-C 等效的内容。

[emitterLayer setValue:@ (scale) forKeyPath:@"emitterCells.cell.emitterCells.childCell.scale"];

我在 Swift 中尝试过以下方法:

emitterLayer.setValue (scale), forKey: "emitterCells.cell.emitterCells.childCell.scale")

但是这不会像在 Objective-C 中那样改变值

这在 Swift 中是否有效,或者我是否需要对其进行重构以以不同的方式工作以达到相同的结果?

【问题讨论】:

  • scale 是值类型吗?将其包裹在 NSNumber 中。
  • forKeyforKeyPath ?
  • 这是 KVC,不是 KVO。

标签: ios objective-c swift key-value-coding caemittercell


【解决方案1】:

应该可以,但是语法是

emitterKayer.setValue(scale, 
  forKeyPath:  "emitterCells.cell.emitterCells.childCell.scale")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2017-12-17
    • 2012-03-29
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多