如何在Swift中声明特定类类型的枚举? [英] How to declare enums in Swift of a particular class type?
问题描述
我正在尝试创建一个类,并将该类用作新枚举的类型,如下所示。
I am trying to create a class and use that class as the type for my new enum like shown below.
class Abc{
var age = 25
var name = "Abhi"
}
enum TestEnum : Abc {
case firstCase
case secondCase
}
我在操场上遇到了错误。
I am getting following error in playground .
error: raw type 'Abc' is not expressible by any literal
所以我尝试遵循这样的RawRepresentable协议。
So i tried conforming to RawRepresentable protocol like this.
extension TestEnum : RawRepresentable{
typealias RawValue = Abc
init?(rawValue:RawValue ) {
switch rawValue {
case Abc.age :
self = .firstCase
case Abc.name :
self = .secondCase
}
}
var rawValue : RawValue {
switch self{
case .firstCase :
return Abc.age
case .secondCase :
return Abc.name
}
}
}
此后,我得到以下错误:
I am getting following errors after this :
error: raw type 'Abc' is not expressible by any literal
error: instance member 'age' cannot be used on type 'Abc'
error: instance member 'name' cannot be used on type 'Abc'
声明某种类类型的枚举的正确方法是什么,对此尚不清楚。有人帮忙吗?
What is the proper way to declare enums of a certain class type, not getting clear idea on this. Anyone help?
推荐答案
从文档中
尤其是,原始值类型必须符合Equatable
协议和以下协议之一:
ExpressibleByIntegerLiteral用于整数文字,
ExpressibleByFloatLiteral用于浮点文字,
ExpressibleByStringLiteral用于包含任意数量的
字符的字符串文字,ExpressibleByUnicodeScalarLiteral或
ExpressibleByExtendedGraphemeClusterLiteral用于
仅包含单个字符的字符串文字。
In particular, the raw-value type must conform to the Equatable protocol and one of the following protocols: ExpressibleByIntegerLiteral for integer literals, ExpressibleByFloatLiteral for floating-point literals, ExpressibleByStringLiteral for string literals that contain any number of characters, and ExpressibleByUnicodeScalarLiteral or ExpressibleByExtendedGraphemeClusterLiteral for string literals that contain only a single character.
因此,使您的Abc类符合Equatable和上述协议之一。这是一个示例
So make your class Abc to conform to Equatable and one of the above mentioned protocols. Here is an example
public class Abc : Equatable,ExpressibleByStringLiteral{
var age = 25
var name = "Abhi"
public static func == (lhs: Abc, rhs: Abc) -> Bool {
return (lhs.age == rhs.age && lhs.name == rhs.name)
}
public required init(stringLiteral value: String) {
let components = value.components(separatedBy: ",")
if components.count == 2 {
self.name = components[0]
if let age = Int(components[1]) {
self.age = age
}
}
}
public required convenience init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
public required convenience init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
}
enum TestEnum : Abc {
case firstCase = "Jack,29"
case secondCase = "Jill,26"
}
现在,您可以像
let exEnum = TestEnum.firstCase
print(exEnum.rawValue.name) // prints Jack
有关详细讨论和示例,请参考
https://swiftwithsadiq.wordpress.com/ 2017/08/21 / custom-types-as-swift的枚举值/
For detailed discussion and example you can refer https://swiftwithsadiq.wordpress.com/2017/08/21/custom-types-as-raw-value-for-enum-in-swift/
这篇关于如何在Swift中声明特定类类型的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!