noname noname - 30 days ago 8
Swift Question

Enum switch via tuple

I am missing synatax for tuple as a case for switch enum.

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")]
}
}


In case with two conditions I am getting an error:
"Tuple pattern cannot match values of the non-tuple type 'MyType.Query'"


Is it even possible ?

EDIT

What about creating
case ownerIsSent(String, Bool)
, then in
switch
as a

case .ownerIsSent(let value1, let value2):
return NSPredicate(format: "owner == %@ AND isSent == %@", value1, NSNumber(booleanLiteral: value2))


Thanks in advance!

Answer Source

the edit would work. I'd rename stuff only:

e.g.

case id(Int)
case sentByOwner(String,Bool)

You want to use Query as a factory for predicates I guess!? Then ... I think you could do this