何时使用didset或在使用@IBInspectable时设置

时间:2016-06-10 03:37:57

标签: swift

看了不同的教程。我不知道何时使用didset或设置更新变量。 任何人都可以解释一下何时使用didset或get set的更多细节?

 @IBInspectable var circleColor: UIColor = UIColor.redColor() {
    didSet { //after properties are set in storyboard, update here
        circleLayer.strokeColor = circleColor.CGColor
        self.toggleButon()
    }
}
/**
    Radius of RadioButton circle.
*/
@IBInspectable var circleRadius: CGFloat = 5.0
@IBInspectable var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
        layer.masksToBounds = newValue > 0
    }
}

对于圆半径,它不必使用didset来更新其值。我无法得到它。

3 个答案:

答案 0 :(得分:0)

我在这里给你一个例子并尝试解释你如何使用,希望它对你有所帮助。

我在UIView这里使用这个类来设置并使用Story Board设置我的类名称“MyCustomView”

import Foundation
import UIKit
import QuartzCore

/// Computed properties, based on the backing CALayer property, that are visible in Interface Builder.
@IBDesignable public class MyCustomView: UIView {
    /// When positive, the background of the layer will be drawn with rounded corners. Also effects the mask generated by the `masksToBounds' property. Defaults to zero. Animatable.
    @IBInspectable var cornerRadius: Double {
        get {
            return Double(self.layer.cornerRadius)
        }
        set {
            self.layer.cornerRadius = CGFloat(newValue)
        }
    }

    /// The width of the layer's border, inset from the layer bounds. The border is composited above the layer's content and sublayers and includes the effects of the `cornerRadius' property. Defaults to zero. Animatable.
    @IBInspectable var borderWidth: Double {
        get {
            return Double(self.layer.borderWidth)
        }
        set {
            self.layer.borderWidth = CGFloat(newValue)
        }
    }

    /// The color of the layer's border. Defaults to opaque black. Colors created from tiled patterns are supported. Animatable.
    @IBInspectable var borderColor: UIColor? {
        get {
            return UIColor(CGColor: self.layer.borderColor!)
        }
        set {
            self.layer.borderColor = newValue?.CGColor
        }
    }

    /// The color of the shadow. Defaults to opaque black. Colors created from patterns are currently NOT supported. Animatable.
    @IBInspectable var shadowColor: UIColor? {
        get {
            return UIColor(CGColor: self.layer.shadowColor!)
        }
        set {
            self.layer.shadowColor = newValue?.CGColor
        }
    }

    /// The opacity of the shadow. Defaults to 0. Specifying a value outside the [0,1] range will give undefined results. Animatable.
    @IBInspectable var shadowOpacity: Float {
        get {
            return self.layer.shadowOpacity
        }
        set {
            self.layer.shadowOpacity = newValue
        }
    }

    /// The shadow offset. Defaults to (0, -3). Animatable.
    @IBInspectable var shadowOffset: CGSize {
        get {
            return self.layer.shadowOffset
        }
        set {
            self.layer.shadowOffset = newValue
        }
    }

    /// The blur radius used to create the shadow. Defaults to 3. Animatable.
    @IBInspectable var shadowRadius: Double {
        get {
            return Double(self.layer.shadowRadius)
        }
        set {
            self.layer.shadowRadius = CGFloat(newValue)
        }
    }
}

你可以将它与故事板一起使用你的“UIView”

导入这个课程

enter image description here

之后你会看到一些 你可以直接在这里设置视角半径,阴影和阴影

enter image description here

结果您可以直接在故事板中查看而无需运行代码

从此代码输出

enter image description here

答案 1 :(得分:0)

This answer非常清楚地解释了setdidSet的使用差异。

摘要

在设置值之前,应使用

willSet来执行某些操作。 (此时不更新该值)。

set是更新值

didSet如果您想在设置值后执行任何操作(此时值已更新)

同时

如果您实施set,则还需要实施get

但也可以使用didSet而无需实施任何其他方法

答案 2 :(得分:0)

@IBInspectable 将与 两种属性一起使用:

  • 将didSet {}用于 存储的属性
    didSet是一个属性观察器。

  • 计算属性 使用 set {} get {}

    < / li>


在以下示例中:firstName和lastName是存储的属性。

fullName是一个计算属性:

struct Person{
    var firstName:String
    var lastName:String{
        didSet{
            //property observer
            print("It was set")
        }
    }

    var fullName:String{
        get{
            return "\(firstName)-\(lastName)"
        }
        set{
            let split = newValue.split(separator: "-")

            firstName = String(split.first ?? "")
            lastName = String(split.last ?? "")
        }
    }
}

var p = Person(firstName: "Moshe", lastName: "Doe")

print(p.firstName)
print(p.lastName)
print(p.fullName)

p.fullName = "Jhon-Doe"

print(p.firstName)
print(p.lastName)
print(p.fullName)

也请查看语言指南:(属性)

https://docs.swift.org/swift-book/LanguageGuide/Properties.html

关于属性的最后说明和 @IBInspectable
可以使用计算属性与存储属性(Backing属性)的组合来实现值的验证:这是一个示例:

//Bound between 0 and 5
var boundRating:Int = 3{
    didSet{
        renderRating()
    }
}

@IBInspectable
var rating:Int{
    set{
        if newValue <= 5 && newValue >= 0{
            boundRating = newValue
            renderRating()
        }
    }
    get{
        return boundRating
    }
}