2017-08-24 113 views
0

我缺少元组的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)) 

感谢什么!

+0

你正在'开关'自己','类型'查询' - 不是元组'(查询,查询)'。 –

+0

@NicolasMiari Got ya !,所以我需要的是:开关(自我,自我)? – noname

+0

我不认为这会起作用; “自我”只能是可能的情况之一。我不认为元组是要走的路;也许你需要重新考虑你的数据类型 –

回答

2

编辑会工作。我只会重命名的东西:

例如

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

你想使用查询作为工厂预测我猜!? 然后...我认为你可以做到这一点

+0

或者也许他可以使用选项集来聚合多个案例? –

+0

以及如果只有一个非常有限的组合数量查询我不会允许任意组合,但它应该工作正常我同意 –

2

这是不可能的。您只能同时切换一个的情况。即使(self, self)也会打开同一个案例。

一个解决办法是有两个相关值增加的情况下,例如

case id(Int) 
case owner(String) 
case isSent(Bool) 
case ownerIsSent(String, Bool)