Swift 5中具有动态值的Initialise属性包装器

问题描述 投票:0回答:1

场景:

  1. 我想使用不同类型的债券,其债券的最小值,弹性利率当前债券价值(根据某些逻辑计算)和债券增长预测。

  2. 无论何时债券价值超过最小值,则增长预测变为反之亦然。

  3. 我可以有多个结合自己的一套价值观。

我正在尝试使用属性包装器来实现相同的目的,以便不对每个键复制相同的行为:

 @propertyWrapper
struct BondValue {
    private var bondCurrentWorth: Int
    private var minimumValue: Int
    private var flexiInterest: Int

    var projectedValue: BondGrowth = .appreciating // The projected value projects the growth for the bond

    var wrappedValue: Int {

        get {
            return bondCurrentWorth
        }

        set {
            bondCurrentWorth = newValue + (newValue * flexiInterest/100)
            projectedValue = bondCurrentWorth < minimumValue ? .depriciating : .appreciating
        } 
 }

    init(wrappedValue: Int = 0, minimumValue: Int, flexiInterest: Int, value: Int) {
        self.minimumValue = minimumValue
        self.flexiInterest = flexiInterest
        self.bondCurrentWorth = value
    }
}

不创建任何键,我这样做:

struct FoodBond {
    var bondName: String
    @BondValue(minimumValue: 200, flexiInterest: 30, value: 200) var value = 30

    init(bondName: String, minimumValue: Int, flexiInterest: Int, value: Int) {
        self.bondName = bondName
    }
}

问题陈述:-我无法使用动态值初始化bondvalue。

可能的解决方法:在下面的实现中,我将使用与普通结构相同的方法。尽管在文档中也指定了使用属性包装器的这种方式,但但是我会放弃属性包装器的语法风格。

您可以编写使用属性包装器行为的代码,无需利用特殊属性语法。例如,这是先前代码清单中的SmallRectangle版本,将其属性显式地包装在TwelveOrLess结构中@TwelveOrLess作为属性的概念

@propertyWrapper
struct BondValue {
    private (set) var bondCurrentWorth: Int
    private (set) var minimumValue: Int
    private (set) var flexiInterest: Int

    var projectedValue: BondGrowth = .appreciating // The projected value projects the growth for the bond

    var wrappedValue: Int { .... }

    init(wrappedValue: Int = 0, minimumValue: Int, flexiInterest: Int, value: Int) { ... }
}

struct FoodBond {
    var bondName: String
    var value = BondValue(minimumValue: 0, flexiInterest: 0, value: 0) // Provide a default value

    init(bondName: String, minimumValue: Int, flexiInterest: Int, value: Int) {
        self.bondName = bondName
        self.value = BondValue(minimumValue: minimumValue, flexiInterest: flexiInterest, value: value)
    }
}

var foodBond = FoodBond(bondName: "Food Bond", minimumValue: 200, flexiInterest: 10, value: 200)
print("Initial bond current worth - \(foodBond.value.bondCurrentWorth)")
print("Bond growth - \(foodBond.value.projectedValue)")

任何建议都会非常有帮助。

ios properties wrapper swift5.1
1个回答
0
投票

关于属性包装器,请参见Swift提案文档的Memberwise Initialisers部分:

具有属性包装器的实例属性将具有一个成员初始化器中的相应参数,其类型将为可以是原始属性类型或包装器类型,具体取决于包装器类型和初始值(如果提供)。

解决方案:

struct FoodBond {
    var bondName: String

    @BondValue(minimumValue: 0, flexiInterest: 0, value: 0) var bondValue = 0

    init(bondName: String, minimumValue: Int, flexiInterest: Int, value: Int) {
        self.bondName = bondName
        _bondValue = BondValue(minimumValue: minimumValue, flexiInterest: flexiInterest, value: value)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.