我缺少元组的synatax作为开关枚举的情况。通过元组的枚举开关
public protocol QueryType {
var predicate: NSPredicate? { get }
var sortDescriptors: [SortDescriptor] { get }
}
public enum Query: QueryType {
case id(Int)
case owner(String)
case isSent(Bool)
public var predicate: NSPredicate? {
switch self {
case .id(let value):
return NSPredicate(format: "id == %d", value)
case (.owner(let value1), .isSent(let value2)):
return NSPredicate(format: "owner == %@ AND isSent == %@", value1, NSNumber(booleanLiteral: value2)
}
}
public var sortDescriptors: [SortDescriptor] {
return [SortDescriptor(keyPath: "id")]
}
}
如果有两个条件,我得到一个错误:"Tuple pattern cannot match values of the non-tuple type 'MyType.Query'"
它甚至有可能?
编辑
约提前switch
创建case ownerIsSent(String, Bool)
,那么作为
case .ownerIsSent(let value1, let value2):
return NSPredicate(format: "owner == %@ AND isSent == %@", value1, NSNumber(booleanLiteral: value2))
感谢什么!
你正在'开关'自己','类型'查询' - 不是元组'(查询,查询)'。 –
@NicolasMiari Got ya !,所以我需要的是:开关(自我,自我)? – noname
我不认为这会起作用; “自我”只能是可能的情况之一。我不认为元组是要走的路;也许你需要重新考虑你的数据类型 –