如何要求在 Swift 协议中定义枚举
How to require an enum be defined in Swift Protocol
是否可以让协议要求定义枚举?
//trying to do this
protocol JSONEncodable {
enum PropertyName // Type not allowed here
func valueForProperty(propertyName:PropertyName) -> Any
}
//which would be implemented like this
struct Person : JSONEncodable {
var firstName : String
var lastName : String
enum PropertyName {
case FirstName
case LastName
func allValues() {
return [Person.PropertyName.FirstName, Person.PropertyName.LastName]
}
func stringValue() {
return "\(self)"
}
}
func valueForProperty(propertyName:PropertyName) -> Any {
switch propertyName {
case .FirstName:
return firstName
case .LastName:
return lastName
}
}
}
//so that I could do something like this
extension JSONEncodable {
func JSONObject() -> [String:AnyObject] {
var dictionary = [String:AnyObject]()
for propertyName in PropertyName.allValues {
let value = valueForProperty(propertyName)
if let valueObject = value as? AnyObject {
dictionary[propertyName.stringValue()] = valueObject
}else if let valueObject = value as? JSONEncodable {
dictionary[propertyName.stringValue()] = valueObject.JSONObject()
}
}
return dictionary
}
}
协议可以有 associatedtypes
这只需要在任何子类中遵守:
enum MyEnum: String {
case foo
case bar
}
protocol RequiresEnum {
associatedtype SomeEnumType: RawRepresentable where SomeEnumType.RawValue: StringProtocol
func doSomethingWithEnum(someEnumType: SomeEnumType)
}
class MyRequiresEnum: RequiresEnum {
typealias SomeEnumType = MyEnum
func doSomethingWithEnum(someEnumType: SomeEnumType) {
switch someEnumType {
case .foo:
print("foo")
case .bar:
print("bar")
}
}
}
let mre = MyRequiresEnum()
mre.doSomethingWithEnum(.bar)
编辑:associatedtype
必须遵守
我认为您可以使用符合 RawRepresentable
的 associatedtype
这是一个例子:
protocol SomeProtocol {
associatedtype SomeType: RawRepresentable
}
如果您需要指定 RawRepresentable
的类型,例如 String
,您可以这样做:
protocol SomeProtocol {
associatedtype SomeType: RawRepresentable where SomeType.RawValue: StringProtocol
}
现在,如果您尝试使用具有 String
作为 RawRepresentable
的 enum
以外的任何其他方式来实现协议,您将遇到编译器错误。
希望对你有帮助。
是否可以让协议要求定义枚举?
//trying to do this
protocol JSONEncodable {
enum PropertyName // Type not allowed here
func valueForProperty(propertyName:PropertyName) -> Any
}
//which would be implemented like this
struct Person : JSONEncodable {
var firstName : String
var lastName : String
enum PropertyName {
case FirstName
case LastName
func allValues() {
return [Person.PropertyName.FirstName, Person.PropertyName.LastName]
}
func stringValue() {
return "\(self)"
}
}
func valueForProperty(propertyName:PropertyName) -> Any {
switch propertyName {
case .FirstName:
return firstName
case .LastName:
return lastName
}
}
}
//so that I could do something like this
extension JSONEncodable {
func JSONObject() -> [String:AnyObject] {
var dictionary = [String:AnyObject]()
for propertyName in PropertyName.allValues {
let value = valueForProperty(propertyName)
if let valueObject = value as? AnyObject {
dictionary[propertyName.stringValue()] = valueObject
}else if let valueObject = value as? JSONEncodable {
dictionary[propertyName.stringValue()] = valueObject.JSONObject()
}
}
return dictionary
}
}
协议可以有 associatedtypes
这只需要在任何子类中遵守:
enum MyEnum: String {
case foo
case bar
}
protocol RequiresEnum {
associatedtype SomeEnumType: RawRepresentable where SomeEnumType.RawValue: StringProtocol
func doSomethingWithEnum(someEnumType: SomeEnumType)
}
class MyRequiresEnum: RequiresEnum {
typealias SomeEnumType = MyEnum
func doSomethingWithEnum(someEnumType: SomeEnumType) {
switch someEnumType {
case .foo:
print("foo")
case .bar:
print("bar")
}
}
}
let mre = MyRequiresEnum()
mre.doSomethingWithEnum(.bar)
编辑:associatedtype
必须遵守
我认为您可以使用符合 RawRepresentable
associatedtype
这是一个例子:
protocol SomeProtocol {
associatedtype SomeType: RawRepresentable
}
如果您需要指定 RawRepresentable
的类型,例如 String
,您可以这样做:
protocol SomeProtocol {
associatedtype SomeType: RawRepresentable where SomeType.RawValue: StringProtocol
}
现在,如果您尝试使用具有 String
作为 RawRepresentable
的 enum
以外的任何其他方式来实现协议,您将遇到编译器错误。
希望对你有帮助。