【问题标题】:Swift 3.0 - Calling functions based on each case of 2 different enumsSwift 3.0 - 基于 2 个不同枚举的每种情况调用函数
【发布时间】:2017-02-11 15:49:08
【问题描述】:

我是新手,还在学习。为了练习,我想尝试一个想法,看看我是否能够自己建模。例如,如果我在枚举中有 2 组数据

protocol Pet {
   var type: PetType { get }
}

protocol PetType {}

enum Cat: PetType {
   case indoor
   case outdoor
}

enum Dog: PetType {
   case small
   case medium
   case large
}

struct MyPet: Pet {
   let type: PetType
   let age: Int
}

对于每种情况,都会执行一个计算人类年龄的特定函数。所以如果我创建一个实例

let garfield = myPet(type: Cat.indoor, age: 3)
myPet.inHumanYears

它应该执行正确的功能。

我尝试了一些东西,但目前我的知识似乎有限。谁能指导/教我如何解决这个问题?

谢谢。

【问题讨论】:

    标签: ios swift function enums model


    【解决方案1】:

    enum 是 swift 中一个强大的类型。真正酷的是它可以具有取决于self(大小写)的功能和属性。通过向枚举添加一些属性,您可以做您想做的事情。

    您的代码将如下所示:

    protocol Pet {
       var type: PetType { get }
    }
    
    protocol PetType {
      func inHumanYears(age: Int) -> Int
    }
    
    enum Cat: PetType {
       case indoor
       case outdoor
    
       func inHumanYears(age: Int) -> Int {
         switch self {
         case .indoor:
           //return you calculations for indoor
         case .outdoor:
             //return you calculations for outdoor
         }
       }
    }
    
    enum Dog: PetType {
       case small
       case medium
       case large
    
       func inHumanYears(age: Int) -> Int {
         switch self {
         case .small:
           //return you calculations for small
         case .large:
            //return you calculations for large
         case .medium:
           //return you calculations for medium
         }
       }
    }
    
    struct MyPet: Pet {
       let type: PetType
       let age: Int
    
       var inHumanYears: Int {
         return type.inHumanYears(age: age)
       }
    }
    

    PetType 检查它本身是枚举的哪种情况并进行相应的计算。然后你可以这样做:

    let garfield = MyPet(type: Cat.indoor, age: 3)
    print(garfield.inHumanYears)
    

    希望对你有帮助!

    【讨论】:

    • 这确实有帮助,谢谢。我可以在每种情况下使用 if else 语句,还是只能返回一个值。因为根据年龄范围的不同,每个案例的计算都会有所不同。
    • 当然可以。你可以在每个案例中做任何你想做的事情,比如进一步的逻辑。
    • 这很好:) 只是出于好奇,这是最好的方法吗?使用 if else 进行计算,还是有更多方法可以做到这一点?我有很多东西要学。
    • 我不确定这个业务逻辑的计算实际上是如何工作的,所以我真的不能推荐任何东西。如果它们非常容易,也许这是使用 if else 的好方法。如果它非常麻烦并且需要很多步骤,那么抽象会更好。嘿别担心,每个人都有很多东西要学:)
    【解决方案2】:

    有几种方法。如果人类年龄是唯一的(即,室外猫的人类年龄永远不会与室内猫相同),您可以扩展 Int 并仅使用 rawValue。否则,您的方法是正确的,您可以这样做:

    独特的人类岁月

    import UIKit
    
    protocol Pet {
        var type: PetType { get }
    }
    
    protocol PetType {
        var humanYears: Int { get}
    }
    
    enum Cat: Int, PetType {
        case indoor = 1
        case outdoor = 10
        
        var humanYears: Int {
            return self.rawValue
        }
    }
    
    enum Dog: Int, PetType {
        case small = 40
        case medium = 1
        case large = 4
        
        var humanYears: Int {
            return self.rawValue
        }
        
    }
    
    struct MyPet: Pet {
        let type: PetType
        let age: Int
    }
    
    let garfield = MyPet(type: Cat.indoor, age: 3)
    garfield.type.humanYears
    

    重复人类年数

    import UIKit
    
    protocol Pet {
        var type: PetType { get }
    }
    
    protocol PetType {
        var humanYears: Int { get}
    }
    
    enum Cat: PetType {
        case indoor
        case outdoor
        
        var humanYears: Int {
            switch self {
            case .indoor: return 4
            case .outdoor: return 5
            }
        }
    }
    
    enum Dog: PetType {
        case small
        case medium
        case large
        
        var humanYears: Int {
            switch self {
            case .small:
                if (yearIs1960) {
                    return 10
                }
                else {
                    return 8
                }
            case .medium: return 5
            case .large: return 5
            }
        }
    }
    
    struct MyPet: Pet {
        let type: PetType
        let age: Int
    }
    
    let garfield = MyPet(type: Cat.indoor, age: 3)
    garfield.type.humanYears
    

    这是 Benedikt Terhechte 撰写的真正的 good article,其中深入介绍了枚举可能性的示例

    【讨论】:

    • 谢谢!我认为这两个想法对我都有很大帮助,我希望在每个案例中都有一个 if else 语句,因为年龄可以是任何东西。
    • 我已经更新了关于 Dog 枚举的帖子,以展示你将如何做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2016-01-05
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    相关资源
    最近更新 更多