我喜欢从枚举中分离soms函数和var,并认为这是一种方法。(只是示例代码)这将导致编译错误"类型'Self'没有成员'allCases'">
public protocol EnumFunctions: Hashable {
static var numOfCases: Self { get }
}
public extension EnumFunctions {
static var numOfCases: Self {
return self.allCases.count
}
}
my Enum Cooking Timer
public struct Cook_Time {
// struct naming for the dump command like
// dump(Cook_Time(), name: Cook_Time().structname)
let structname : String = "Cook_Time"
let a = "a"
let allValues = PFTime.allValues
public enum PFTime : String , CaseIterable, EnumFunctions {
case t1m = "1mim"
case t3m = "3min"
case t5m = "5min"
case t10m = "10min"
....
static let allValues = PFTimer.allCases.map { $0.rawValue }
}
}
我该如何解决这个问题?我的错误想法是什么?我需要Self而不是Self,对吧?
另外,如果我为PFTime做扩展,在一个单独的文件中,为什么我得到错误"无法在范围"中找到类型'PFTime' ?
为了访问CaseIterable
协议的allCases
属性,您需要将EnumFunctions
协议更改为如下内容:
public protocol EnumFunctions: Hashable, CaseIterable {
static var numOfCases: Int { get }
}
public extension EnumFunctions {
static var numOfCases: Int {
return allCases.count
}
}
也可以通过添加Cook_Time.
作为前缀来创建PFTime
的扩展,因为PFTime
被放置在Cook_Time
结构中:
extension Cook_Time.PFTime {
}