[英]Swift understand generics with enums
I have following code written by other person: 我有其他人写的以下代码:
public enum MSEntity<T : Metable> {
case meta(MSMeta)
case entity(T)
public func value() -> T? {
guard case let MSEntity.entity(value) = self else {
return nil
}
return value
}
public func objectMeta() -> MSMeta {
switch self {
case .entity(let entity): return entity.meta
case .meta(let meta): return meta
}
}
}
I have following questions: 我有以下问题:
1) what is the point of doing case entity(T)? 1)做案例实体(T)有什么意义? What is "value" of that enum case and for what it may be used for? 该枚举实例的“值”是什么,以及它的用途是什么?
2) I can't understand public func value()
func. 2)我无法理解public func value()
函数。 What is that guard checking? 那个警卫检查什么? What is value? 有什么价值? Can someone provide an example where similar code may be useful? 有人可以提供类似代码可能有用的示例吗?
The enum seems to represent an object that can either only contain metadata or a entity with metadata. 枚举似乎表示只能包含元数据或带有元数据的实体的对象。
It has 2 cases: meta
represents an object with only metadata and that metadata is this case's associated value, entity
represents an object that has a entity and metadata. 它有2种情况: meta
表示仅包含元数据的对象,而元数据是此案例的关联值, entity
表示具有实体和元数据的对象。 This case's associated value is the object's entity, which contains metadata (so the constraint on T
is Metable
) 该案例的关联值是对象的实体,其中包含元数据(因此对T
的约束是Metable
)
what is the point of doing case entity(T)? 做案例实体(T)有什么意义? What is "value" of that enum case and for what it may be used for? 该枚举实例的“值”是什么,以及它的用途是什么?
Well, for this you have to look at the docs or ask the person who wrote the code. 好吧,为此,您必须查看文档或询问编写代码的人。 As far as I'm concerned, MSEntity
is an object that has 2 states: 就我而言, MSEntity
是一个具有2种状态的对象:
meta
case) 仅元数据( meta
大小写) entity
case) 实体和元数据( entity
案例) So the entity
case is there to represent that. 因此, entity
案例可以代表这一点。
I can't understand public func value() func. 我无法理解public func value()函数。 What is that guard checking? 那个警卫检查什么? What is value? 有什么价值?
The value
function seems to return the object's entity, but what if this object has no entity (ie self
is actually in the meta
case)? value
函数似乎返回了对象的实体,但是如果该对象没有实体(即self
实际上在meta
情况下)怎么办? The author of this code decided to return nil
. 该代码的作者决定返回nil
。 That is why there is a check: if self
is in the meta
case. 这就是为什么要检查的原因: self
是否在meta
情况下。 The word value
is part of the pattern matching. 字value
是模式匹配的一部分。 It might be clearer if I wrote it like this: 如果我这样写,可能会更清楚:
guard case let MSEntity.entity(let value) = self else {
Whatever is in the associated value of entity
case is put into a variable called value
, so that you can return it later. entity
大小写的关联值中的任何值都放入一个名为value
的变量中,以便以后可以将其返回。
The (MSMeta)
and (T)
associated values of the meta
and entity
cases, respectively. meta
案例和entity
案例的(MSMeta)
和(T)
关联值分别。
The value()
method (which IMO should be a computed property of type T?
, rather than a function of type () -> T?
) returns the associated value of the entity
case, if self
is entity
, otherwise it returns nil
(when the self
is meta
). value()
方法(该IMO应该是T?
类型的计算属性,而不是类型() -> T?
的函数)将返回entity
案例的关联值(如果self
为entity
,否则返回nil
(当self
是meta
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.