Swift: Enums that use closures?

前端 未结 2 1239
轮回少年
轮回少年 2021-02-13 02:31

I’m creating an application where an unlimited amount of rules could be applied to an unlimited amount of nodes.

I\'m planning on using core data as the datastore and

2条回答
  •  借酒劲吻你
    2021-02-13 03:23

    Solution

    If we want to add a closure to an enum first of all lets define the type of the closure.

    typealias Logic = () -> (String)
    

    Then the enum:

    enum Rule {
        case SimpleRuleA(Logic)
        case SimpleRuleB(Logic)
        case BigFatComplicatedRule(Logic)
    }
    

    That's it! Let's see now how to use this.

    Usage

    Let's create a couple of Logic(s):

    let logic0 : Logic = { return "Logic 0" }
    let logic1 : Logic = { return "Logic 1" }
    

    And now a function to process a Rule

    func processRule(rule:Rule) -> String {
        switch rule {
        case .SimpleRuleA(let logic): return "Simple Rule A, logic: \(logic())"
        case .SimpleRuleB(let logic): return "Simple Rule B, logic: \(logic())"
        case .BigFatComplicatedRule(let logic): return "Big Fat Complicated Rule, logic: \(logic())"
        }
    }
    

    Finally let's combine every possible rule with every possible Logic...

    let aWithLogic0 = Rule.SimpleRuleA(logic0)
    let aWithLogic1 = Rule.SimpleRuleA(logic1)
    let bWithLogic0 = Rule.SimpleRuleB(logic0)
    let bWithLogic1 = Rule.SimpleRuleB(logic1)
    let fatWithLogic0 = Rule.BigFatComplicatedRule(logic0)
    let fatWithLogic1 = Rule.BigFatComplicatedRule(logic1)
    

    ... and let's test it

    processRule(aWithLogic0) // "Simple Rule A, logic: Logic 0"
    processRule(aWithLogic1) // "Simple Rule A, logic: Logic 1"
    processRule(bWithLogic0) // "Simple Rule B, logic: Logic 0"
    processRule(bWithLogic1) // "Simple Rule B, logic: Logic 1"
    processRule(fatWithLogic0) // "Big Fat Complicated Rule, logic: Logic 0"
    processRule(fatWithLogic1) // "Big Fat Complicated Rule, logic: Logic 1"
    

    Is this solution close to what you had in mind?

提交回复
热议问题