1

我需要一个 Swift 属性——如果该值尚未设置——默认为另一个值。

可以使用后备存储私有属性来实现。例如,对于num应该默认为 global的属性defaultNum,它的工作方式如下:

var defaultNum = 1

class MyClass {
  var num: Int {
    get { _num ?? defaultNum }
    set { _num = newValue }
   }

  private var _num: Int?
}

let c = MyClass()
print("initial \(c.num)") // == 1 ✅

// changing the default changes the value returned
defaultNum = 2
print("dynamic \(c.num)") // == 2 ✅

// once the property is set, returns the stored value
c.num = 5
print("base    \(c.num)") // == 5 ✅

这行得通,但是对于我们代码中的一个常见模式,每个这样的属性都有很多样板文件。

使用 Swift 属性包装器,是否可以更简洁地做到这一点?


什么行不通_

请注意,因为我们希望默认值是动态的,所以静态初始化器将不起作用。例如:

var defaultNum = 1

class MyClass {
  var num = defaultNum
}

var c = MyClass()
defaultNum = 2
print(c.num) // this == 1, we want the current value of defaultNum, which == 2
4

1 回答 1

3

您可以通过创建这样的属性包装器来做到这一点:

@propertyWrapper
public struct Default<T> {
  var baseValue: T?
  var closure: () -> T

  // this allows a nicer syntax for single variables...
  public init(_ closure: @autoclosure @escaping () -> T) {
    self.closure = closure
  }

  // ... and if we want to explicitly use a closure, we can.
  public init(_ closure: @escaping () -> T) {
    self.closure = closure
  }

  public var wrappedValue: T {
    get { baseValue ?? closure() }
    set { baseValue = newValue }
  }
}

然后,您可以在这样的@Default属性上使用属性包装器:

var defaultNum = 1

class MyClass {
  @Default(defaultNum)
  var num: Int
}

然后,您会在实践中看到以下内容:

let c = MyClass()

// if we haven't set the property yet, it uses the closure to return a default value
print("initial \(c.num)") // == 1 ✅

// because we are using a closure, changing the default changes the value returned
defaultNum = 2
print("dynamic \(c.num)") // == 2 ✅

// once the property is set, uses the stored base value
c.num = 5
print("base    \(c.num)") // == 5 ✅
于 2019-11-18T20:22:27.747 回答