swift中的困难类型

swift中的困难类型,swift,struct,enums,tuples,Swift,Struct,Enums,Tuples,我正在尝试做一个“困难”类型,它可以分为三种状态:简单、中等或困难。然后将自动设置“最小”和“最大”值,并可访问,如myDifficultyInstance.min或其他 我尝试了这个,但不起作用,我得到了错误: enum Difficulty { case easy(min: 50, max: 200) case medium(min: 200, max: 500) case hard(min: 500, max: 1000) } 然后我尝试了一个结构,但它变得太怪异和丑陋

我正在尝试做一个“困难”类型,它可以分为三种状态:简单、中等或困难。然后将自动设置“最小”和“最大”值,并可访问,如myDifficultyInstance.min或其他

我尝试了这个,但不起作用,我得到了错误:

enum Difficulty {
   case easy(min: 50, max: 200)
   case medium(min: 200, max: 500)
   case hard(min: 500, max: 1000)
}
然后我尝试了一个结构,但它变得太怪异和丑陋

有没有简单的解决办法

枚举案例中不允许使用默认参数

定义枚举的事例时,不能定义默认值。想象一下,你只是在创建模式

但您可以做的是,您可以通过创建静态常量来创建默认情况

enum Difficulty {
    case easy(min: Int, max: Int)
    case medium(min: Int, max: Int)
    case hard(min: Int, max: Int)

    static let defaultEasy = easy(min: 50, max: 200)
    static let defaultMedium = medium(min: 200, max: 500)
    static let defaultHard = hard(min: 500, max: 1000)
}
那么你可以这样使用它

Difficulty.defaultEasy
Difficulty.defaultMedium
Difficulty.defaultHard
我还认为,对于需要获取最小值或最大值的情况,如果使用自定义数据模型,效果会更好

struct Difficulty {

    var min: Int
    var max: Int

    static let easy = Difficulty(min: 50, max: 200)
    static let medium = Difficulty(min: 200, max: 500)
    static let hard = Difficulty(min: 500, max: 1000) 
}
枚举案例中不允许使用默认参数

定义枚举的事例时,不能定义默认值。想象一下,你只是在创建模式

但您可以做的是,您可以通过创建静态常量来创建默认情况

enum Difficulty {
    case easy(min: Int, max: Int)
    case medium(min: Int, max: Int)
    case hard(min: Int, max: Int)

    static let defaultEasy = easy(min: 50, max: 200)
    static let defaultMedium = medium(min: 200, max: 500)
    static let defaultHard = hard(min: 500, max: 1000)
}
那么你可以这样使用它

Difficulty.defaultEasy
Difficulty.defaultMedium
Difficulty.defaultHard
我还认为,对于需要获取最小值或最大值的情况,如果使用自定义数据模型,效果会更好

struct Difficulty {

    var min: Int
    var max: Int

    static let easy = Difficulty(min: 50, max: 200)
    static let medium = Difficulty(min: 200, max: 500)
    static let hard = Difficulty(min: 500, max: 1000) 
}

我知道您已经接受了答案,但如果您想同时设置预设和自定义难度,我建议您这样做:

enum Difficulty {
   case easy
   case medium
   case hard
   case custom(min: Int, max: Int)

   var min : Int {
       switch self {
       case .easy:
           return 50
       case .medium:
           return 200
       case .hard:
           return 500
       case .custom(let min,_):
           return min
       }
   }

   var max : Int {
       switch self {
       case .easy:
           return 200
       case .medium:
           return 500
       case .hard:
           return 1000
       case .custom(_,let max):
           return max
       }
   }
}
通过这种方式,您可以使用定义自定义状态的选项来枚举有限的独占状态

用法:

let difficulty : Difficulty = .easy
let customDifficulty : Difficulty = .custom(min: 70, max: 240)

let easyMin = difficulty.min
let easyMax = difficulty.max

let customMin = customDifficulty.min
let customMax = customDifficulty.max

我知道您已经接受了答案,但如果您想同时设置预设和自定义难度,我建议您这样做:

enum Difficulty {
   case easy
   case medium
   case hard
   case custom(min: Int, max: Int)

   var min : Int {
       switch self {
       case .easy:
           return 50
       case .medium:
           return 200
       case .hard:
           return 500
       case .custom(let min,_):
           return min
       }
   }

   var max : Int {
       switch self {
       case .easy:
           return 200
       case .medium:
           return 500
       case .hard:
           return 1000
       case .custom(_,let max):
           return max
       }
   }
}
通过这种方式,您可以使用定义自定义状态的选项来枚举有限的独占状态

用法:

let difficulty : Difficulty = .easy
let customDifficulty : Difficulty = .custom(min: 70, max: 240)

let easyMin = difficulty.min
let easyMax = difficulty.max

let customMin = customDifficulty.min
let customMax = customDifficulty.max

你可以在“谢谢”中找到很多例子,我现在查看你可以在“谢谢”中找到很多例子,我现在查看很好的解决方案!递归枚举;美好的那么,如果我想访问示例的最小值,我该怎么做?@plastical我不确定您是否可以使用enum执行此操作,但如果您使用的是自定义模型,则更好Difficulty@Placard更新了我的答案,你可以激发你的自我完善,这正是我想要的!谢谢你的解决方案!递归枚举;美好的那么,如果我想访问示例的最小值,我该怎么做?@plastical我不确定您是否可以使用enum执行此操作,但如果您使用的是自定义模型,则更好Difficulty@Placard更新了我的答案,你可以激发你的自我完善,这正是我想要的!谢谢汉克斯,它也很完美!谢谢,它也很完美!