Swift通用完成处理程序

时间:2016-11-13 07:17:36

标签: swift generics completionhandler

我试图弄清楚如何创建通用的完成处理程序。下面是一个示例说明&#34;内部&#34;通用完成处理程序和相同的通用完成处理程序,如果我可以在&#34; external&#34;中执行此操作,我希望能够创建它。形成。问题是我不知道如何在完成处理程序中编写等效的internalCompletion<T: MyEnum>...。我已经在externalCompletion函数中写了我想象的样子:func externalCompletion(_ completer<T: MyEnum>: ((T) -> Void) where T: Hashable))的内容,但这显然不正确。我试图做的是什么?我的预感是,swift不会让完成处理程序保持通用,总是需要在函数级别进行类型转换,这会破坏我的示例的目的(即func externalCompletetion<T: MyEnum>(_ completer: ((T) -> Void)) where T: Hashable,问题就是这样我会必须在EnumA,EnumB和EnumC之间进行选择,无法在所有三个上运行完成者。)

typealias MyEnumKeyedData<T: MyEnum> = [T: String] where T: Hashable

// MARK : - MyEnum Protocol

protocol MyEnum {
  static func all<T: MyEnum>(_:T.Type) -> [T] where T: Hashable
  static var all: [MyEnum] { get }
}
extension MyEnum {
  static func all<T: MyEnum>(_:T.Type) -> [T] where T: Hashable { return Self.all as! [T] }
}

// MARK : - My enums

enum EnumA: MyEnum {
  case first
  static var all: [MyEnum] { return [EnumA.first]}
}
enum EnumB: MyEnum {
  case first
  static var all: [MyEnum] { return [EnumB.first]}
}
enum EnumC: MyEnum {
  case first
  static var all: [MyEnum] { return [EnumC.first]}
}

// MARK : - MyEnum Data Iterator

class MyDataEnumIterator {
  var dataA: MyEnumKeyedData<EnumA> = [:]
  var dataB: MyEnumKeyedData<EnumB> = [:]
  var dataC: MyEnumKeyedData<EnumC> = [:]

  func updateData<T: MyEnum>(_ key: T, _ value: String) where T: Hashable {
    switch T.self {
    case is EnumA.Type: dataA[key as! EnumA] = value
    case is EnumB.Type: dataB[key as! EnumB] = value
    case is EnumC.Type: dataC[key as! EnumC] = value
    default: fatalError("Enum does not exist")
    }
  }

  // Internal (This works)

  func internalEnumIterator() {
    for key in EnumA.all(EnumA.self) { internalCompletion(key) }
    for key in EnumB.all(EnumB.self) { internalCompletion(key) }
    for key in EnumC.all(EnumC.self) { internalCompletion(key) }
  }

  func internalCompletion<T: MyEnum>(_ key: T) where T: Hashable {
    let value = "\(key)"
    updateData(key, value)
  }

  // External (This obviously doesn't, just sketching the idea)

  func externalEnumIterator(_ completer<T: MyEnum>: ((T) -> Void) where T: Hashable)  {
    for key in EnumA.all(EnumA.self) { completer(key) }
    for key in EnumB.all(EnumB.self) { completer(key) }
    for key in EnumC.all(EnumC.self) { completer(key) }
  }
}

// MARK : - Test cases (internal works, external does not, just sketching example)

let iterator = MyDataEnumIterator()
iterator.externalEnumIterator({ <T: MyEnum> (T) where T: Hashable in
  let value = "\(key)"
  iterator.updateData(key, value)
})
iterator.internalEnumIterator()

1 个答案:

答案 0 :(得分:1)

这是代码的工作版本,只需进行最少的更改即可实现

typealias MyEnumKeyedData<T: MyEnum> = [T: String] where T: Hashable

// MARK : - MyEnum Protocol

protocol MyEnum {
    static func all<T: MyEnum>(_:T.Type) -> [T] where T: Hashable
    static var all: [MyEnum] { get }
}
extension MyEnum {
    static func all<T: MyEnum>(_:T.Type) -> [T] where T: Hashable { return Self.all as! [T] }
}

// MARK : - My enums

enum EnumA: MyEnum {
    case first
    static var all: [MyEnum] { return [EnumA.first]}
}
enum EnumB: MyEnum {
    case first
    static var all: [MyEnum] { return [EnumB.first]}
}
enum EnumC: MyEnum {
    case first
    static var all: [MyEnum] { return [EnumC.first]}
}

// MARK : - MyEnum Data Iterator

class MyDataEnumIterator {
    var dataA: MyEnumKeyedData<EnumA> = [:]
    var dataB: MyEnumKeyedData<EnumB> = [:]
    var dataC: MyEnumKeyedData<EnumC> = [:]

    func updateData(_ key: MyEnum, _ value: String) {
        switch key {
        case let key as EnumA: dataA[key] = value
        case let key as EnumB: dataB[key] = value
        case let key as EnumC: dataC[key] = value
        default: fatalError("Enum does not exist")
        }
    }

    // Internal (This works)

    func internalEnumIterator() {
        for key in EnumA.all(EnumA.self) { internalCompletion(key) }
        for key in EnumB.all(EnumB.self) { internalCompletion(key) }
        for key in EnumC.all(EnumC.self) { internalCompletion(key) }
    }

    func internalCompletion<T: MyEnum>(_ key: T) where T: Hashable {
        let value = "\(key)"
        updateData(key, value)
    }

    func EnumIterator(_ compeltitionHandler: (MyEnum) -> Void) {
        for key in EnumA.all(EnumA.self) { compeltitionHandler(key as MyEnum) }
        for key in EnumB.all(EnumB.self) { compeltitionHandler(key as MyEnum) }
        for key in EnumC.all(EnumC.self) { compeltitionHandler(key as MyEnum) }
    }
}


let iterator = MyDataEnumIterator()

iterator.EnumIterator{ key in
    let value = "\(key)"
    iterator.updateData(key, value)
}
iterator.internalEnumIterator()

这是一个理智的代码版本,可以删除所有废话,并添加下标语法:

// MARK : - MyEnum Protocol

protocol MyEnum {
    static func all() -> [MyEnum]
}

// MARK : - My enums

enum EnumA: MyEnum {
    case first
    static func all() -> [MyEnum] { return [EnumA.first] }
}
enum EnumB: MyEnum {
    case first
    static func all() -> [MyEnum] { return [EnumB.first] }
}
enum EnumC: MyEnum {
    case first
    static func all() -> [MyEnum] { return [EnumC.first] }
}

// MARK : - MyEnum Data Iterator

class MyDataEnumIterator {
    var dataA = [EnumA: String]()
    var dataB = [EnumB: String]()
    var dataC = [EnumC: String]()


    subscript(key: MyEnum) -> String? {
        get {
            switch key {
            case let key as EnumA: return dataA[key]
            case let key as EnumB: return dataB[key]
            case let key as EnumC: return dataC[key]
            default: fatalError("Enum does not exist")
            }
        }
        set {
            switch key {
            case let key as EnumA: dataA[key] = newValue
            case let key as EnumB: dataB[key] = newValue
            case let key as EnumC: dataC[key] = newValue
            default: fatalError("Enum does not exist")
            }
        }
    }

    func EnumIterator(_ body: (MyEnum) -> Void) {
        EnumA.all().forEach(body);
        EnumB.all().forEach(body);
        EnumC.all().forEach(body);
    }
}


let iterator = MyDataEnumIterator()
iterator.EnumIterator{
    iterator[$0] = "\($0)"
}