map (keyPath) โดยที่ keyPath เป็นตัวแปร

Sep 06 2020
let arr = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
arr.map(\.0) // [1, 2, 3, 4, 5]

ใช้งานได้ดี แต่โค้ดด้านล่างไม่ได้รวบรวม:

let keyPath = \(Int, Int).0
arr.map(keyPath)

ไม่สามารถแปลงค่าของประเภท 'WritableKeyPath <(Int, Int), Int>' เป็นประเภทอาร์กิวเมนต์ที่คาดไว้ '((Int, Int)) พ่น -> T'
ไม่สามารถอนุมานพารามิเตอร์ทั่วไป 'T' ได้

คำตอบ

4 NewDev Sep 06 2020 at 20:41

Array.map(Element) throws -> Tคาดว่าจะปิดด้วยลายเซ็น

ใน Swift 5.2 เส้นทางสำคัญได้รับอนุญาตให้ส่งผ่านเป็นฟังก์ชัน / การปิด (นี่คือข้อเสนอวิวัฒนาการ ) แต่เป็นเพียงตัวอักษรเท่านั้น (อย่างน้อยตามข้อเสนอระบุว่า "สำหรับตอนนี้" ดังนั้นข้อ จำกัด นี้อาจถูกยกเลิก ).

เพื่อเอาชนะสิ่งนี้คุณสามารถสร้างส่วนขยายSequenceที่ยอมรับเส้นทางสำคัญ:

extension Sequence {
   func map<T>(_ keyPath: KeyPath<Element, T>) -> [T] {
      return map { $0[keyPath: keyPath] }
   }
}

(เครดิตถึง: https://www.swiftbysundell.com/articles/the-power-of-key-paths-in-swift/)

จากนั้นคุณสามารถทำสิ่งที่คุณต้องการ:

let keyPath = \(Int, Int).0
arr.map(keyPath)
Jessy Sep 06 2020 at 22:24

ข้อเสนอวิวัฒนาการแสดงให้เห็นถึงวิธีดำเนินการกับตัวดำเนินการ แต่คุณยังสามารถใช้ตัวดำเนินการ[]หรือ()ไวยากรณ์เดียวกันได้ไม่ว่าจะใช้บางส่วนหรือไม่ก็ตามเนื่องจากตัวห้อยและฟังก์ชันไม่จำเป็นต้องมีข้อโต้แย้ง

let oneTo5 = 1...5
let keyPath = \(Int, Int).0
XCTAssert(
  zip(oneTo5, oneTo5).map(keyPath[]).elementsEqual(oneTo5)
)
let keyPath = \Double.isZero
XCTAssertFalse(keyPath[1.0]())
public extension KeyPath {
  /// Convert a `KeyPath` to a partially-applied get accessor.
  subscript() -> (Root) -> Value {
    { $0[keyPath: self] } } /// Convert a `KeyPath` to a get accessor. subscript(root: Root) -> () -> Value { { root[keyPath: self] } } } public extension ReferenceWritableKeyPath { /// Convert a `KeyPath` to a partially-applied get/set accessor pair. subscript() -> (Root) -> Computed<Value> { { self[$0] }
  }

  /// Convert a `KeyPath` to a get/set accessor pair.
  subscript(root: Root) -> Computed<Value> {
    .init(
      get: self[root],
      set: { root[keyPath: self] = $0 }
    )
  }
}


/// A workaround for limitations of Swift's computed properties.
///
/// Limitations of Swift's computed property accessors:
/// 1. They are not mutable.
/// 2. They cannot be referenced as closures.
@propertyWrapper public struct Computed<Value> {
  public typealias Get = () -> Value
  public typealias Set = (Value) -> Void

  public init(
    get: @escaping Get,
    set: @escaping Set
  ) {
    self.get = get
    self.set = set
  }

  public var get: Get
  public var set: Set

  public var wrappedValue: Value {
    get { get() }
    set { set(newValue) }
  }

  public var projectedValue: Self {
    get { self }
    set { self = newValue }
  }
}

//MARK:- public
public extension Computed {
  init(
    wrappedValue: Value,
    get: @escaping Get = {
      fatalError("`get` must be assigned before accessing `wrappedValue`.")
    },
    set: @escaping Set
  ) {
    self.init(get: get, set: set)
    self.wrappedValue = wrappedValue
  }
}