无法在 UIView 中设置 @IBInspectable 计算属性 [英] Can't set @IBInspectable computed property in UIView

查看:21
本文介绍了无法在 UIView 中设置 @IBInspectable 计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 UIView 添加 IBInspectable 颜色,以便我可以在故事板中设置它,然后在代码中使用它.在

我崩溃了:无法在 (UIView) 上设置 (additionalColor1) 用户定义的检查属性:[ setValue:forUndefinedKey:]:此类不符合键 additionalColor1 的键值编码.

知道是什么导致了崩溃以及如何修复它吗?

这是我的代码:

扩展 UIView {@IBInspectable var additionalColor1:UIColor?{返回 self.additionalColor1}}

作为参考,我粘贴了可用于为 UITextField 设置占位符颜色的代码(与上面的 url 相同).这工作正常:

扩展 UITextField {@IBInspectable var placeHolderColor: UIColor?{得到 {返回 self.placeHolderColor}放 {self.attributedPlaceholder = NSAttributedString(string: self.placeholder != nil ? self.placeholder!: "", attributes:[NSForegroundColorAttributeName: newValue!])}}}

解决方案

如你的问题标题所述

<块引用>

Swift 扩展只能向类型添加计算属性,但它们不能添加存储属性.

(更多详细信息请参考扩展 The Swift Programming Language 中的一章.)

您发布的示例实际上是有缺陷的——即使此时它在 Stackoverflow 上有 50 个赞成票.如果您从属性的 getter 返回属性本身的值,则您正在创建一个循环.

@IBInspectable var additionalColor1: UIColor?{返回 self.additionalColor1}

如果您有一个 view 并且您尝试访问代码中任何位置的 view.additionalColor1,您的属性的 getter 将被调用,它返回 self.additionalColor1——或者换句话说:它再次返回属性的值——猜猜怎么做?通过调用属性的吸气剂!(等等……)

您提到的帖子中的示例仅有效",因为显然从未调用过getter.只是设置计算属性placeHolderColor改变了另一个存储属性,即文本字段的attributedPlaceholder.

因此,虽然您可以通过扩展向现有类添加计算属性,但您永远不能将其视为存储在某处的具体值.计算属性只能用于以某种方式转换您分配给它的值并将结果存储在现有存储属性中.

I'm trying to add an IBInspectable color to UIView, so that I can set it in the storyboard and later use it in code. In this post regarding UITextField I've seen that I can take advantage of extensions and adding a computed property, but I can't make it work for UIView.

I get a crash: Failed to set (additionalColor1) user defined inspected property on (UIView): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key additionalColor1.

Any idea what's causing the crash and how to fix it?

Here's my code:

extension UIView {
    @IBInspectable var additionalColor1: UIColor? {
        return self.additionalColor1
    }
}

For the reference, I'm pasting the code that can be used to set the placeholder color for UITextField (same as the above url). This works ok:

extension UITextField {
    @IBInspectable var placeHolderColor: UIColor? {
        get {
            return self.placeHolderColor
        }
        set {
            self.attributedPlaceholder = NSAttributedString(string: self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: newValue!])
        }
    }
}

解决方案

As mentioned in your question title

Swift extensions can only add computed properties to a type, but they cannot add stored properties.

(For more detailed information please refer to the Extension chapter in The Swift Programming Language.)

The example you posted is actually flawed — even if it has 50 upvotes on Stackoverflow at this time. If you return the value of a property itself from the property's getter you're creating a loop.

@IBInspectable var additionalColor1: UIColor? {
    return self.additionalColor1
}

If you have a view and you try to access view.additionalColor1 anywhere in your code your property's getter will be called which returns self.additionalColor1 — or with other words: it returns the property's value again — and guess how? By calling the propery's getter! (And so on...)

The example from the post you mentioned only "works" because the getter is evidently never called. It's only setting that computed property placeHolderColor that changes another stored property, namely the text field's attributedPlaceholder.

So while you can add computed properties to an existing class through an extension you can never think of it as a concrete value that's stored somewhere. Computed properties may only be used to somehow transform the value you assign to it and store the result in existing stored properties.

这篇关于无法在 UIView 中设置 @IBInspectable 计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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