Swift:计算属性的Cocoa绑定值不起作用 [英] Swift: Cocoa binding value to a computed property does not work

查看:224
本文介绍了Swift:计算属性的Cocoa绑定值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习KVC和绑定。目前,我试图绑定一个NSTextField到计算属性 colorWallStr 。我已经将滑块的值绑定到相应的颜色变量,并将标签的值绑定到computed属性。



但是,移动幻灯片时,标签的内容不会改变。

  // MainWindowController内部
动态var colorRed:CGFloat = 1.0
动态var colorGreen:CGFloat = 1.0
动态var colorBlue:CGFloat = 0.0

dynamic var colorWallStr:String {
get {
returnR:\(colorRed)G:\(colorGreen)B:\(colorBlue)
}
}




当我将标签直接链接到颜色变量时,它工作正常。





感谢@ vadian的回答。现在我可以使用属性的 didSet 更新标签来触发更新标签方法(见下文)。

  dynamic var colorBlue:CGFloat = 0.0 {
didSet {
updateLabel()
}
}

func updateLabel
colorWall =R:\(colorRed)G:\(colorGreen)B:\(colorBlue)
}

如果字符串插值中使用的属性不更新包含的计算属性,那么为什么以下代码段不起作用?

  dynamic var colorWall:String {
get {
let red = colorRed
let green = colorGreen
let blue = colorBlue
returnR:\(red)G:\(green)B:\(blue)
}
}
pre>

解决方案

Key-Value Observering API允许您注册 em>。下面是文档介绍主题的方法:


在很多情况下,一个属性的值取决于一个或多个其他属性在另一个对象。如果一个属性的值更改,那么派生属性的值也应标记为更改。


在这种情况下, colorWallString 的值对你的三个颜色变量的值,所以你需要做的是实现一个类方法,使这一点清楚:

  //这是非常重要的,你得到这个方法的签名正确,
//否则会被忽略。
class func keyPathsForValuesAffectingColorWallStr() - >设置< NSObject> {
return set< NSObject>(arrayLiteral:colorRed,colorBlue,colorGreen)
}

如代码片段中所述,用于标记从属键的方法的格式至关重要;您可以(而且应该)阅读此处的相关文档


I am learning KVC and binding. Currently, I am trying to bind an NSTextField to a computed property colorWallStr. I have bound the sliders' value to the corresponding color variable and also bound the value of the label to the computed property.

However, the content of the label does not change when I move the slide.

// Inside MainWindowController
dynamic var colorRed: CGFloat = 1.0
dynamic var colorGreen: CGFloat = 1.0
dynamic var colorBlue: CGFloat = 0.0

dynamic var colorWallStr: String {
    get {
        return "R: \(colorRed) G: \(colorGreen) B: \(colorBlue)"
    }
}

It is working fine when I bond the label to the color variable directly.

Thanks @vadian's answer. Now I can update the label using property's didSet to trigger update label method (see below).

dynamic var colorBlue: CGFloat = 0.0 {
    didSet {
        updateLabel()
    }
}

func updateLabel() {
    colorWall = "R: \(colorRed) G: \(colorGreen) B: \(colorBlue)"
}

If properties used in string interpolation don't update the enclosing computed property, then why does the following code snippet does not work?

dynamic var colorWall: String {
    get {
        let red = colorRed
        let green = colorGreen
        let blue = colorBlue
        return "R: \(red) G: \(green) B: \(blue)"
    }
}

解决方案

The Key-Value Observering API lets you handle situations like this by allowing you to register dependent keys. Here's how the documentation introduces the subject:

There are many situations in which the value of one property depends on that of one or more other attributes in another object. If the value of one attribute changes, then the value of the derived property should also be flagged for change.

In this situation the value of colorWallString depends on the value of your three color variables, so all you need to do is implement a class method that makes this clear:

// It's crucial that you get the signature of this method correct, 
// otherwise it'll just be ignored.
class func keyPathsForValuesAffectingColorWallStr() -> Set<NSObject> {
    return Set<NSObject>(arrayLiteral: "colorRed", "colorBlue", "colorGreen")
}

As noted in the code snippet, the format of the method you use to flag up dependent keys is crucial; you can (and should) read the relevant documentation here.

这篇关于Swift:计算属性的Cocoa绑定值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆