Advertisement
Larme

Untitled

Jun 13th, 2022
1,505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.76 KB | None | 0 0
  1. func queryFromCollection<T: Codable>(of type: T.Type, query set: Set<UUID>, for key: String) {
  2.  
  3. //    let objects = await RealmReadService.instance.readObjects(of: type)
  4.     var predicates: [NSPredicate] = []
  5.     for item in set {
  6.         let query = NSPredicate(format: "%K == %@", key, item as CVarArg)
  7.         predicates.append(query)
  8.     }
  9.     let compoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
  10.     print(compoundPredicate)
  11. //    return objects!.filter(compoundPredicate)
  12. }
  13. func queryFromCollection2<T: Codable, S>(of type: T.Type, query set: Set<S>, for key: String) {
  14.     var predicates: [NSPredicate] = []
  15.     for item in set {
  16.         let query = NSPredicate(format: "%K == %@", key, item as! CVarArg)
  17.         predicates.append(query)
  18.     }
  19.     let compoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
  20.     print(compoundPredicate)
  21. //    return objects!.filter(compoundPredicate)
  22. }
  23.  
  24. func queryFromCollection3<T: Codable, S>(of type: T.Type, query set: Set<S>, for key: String) {
  25.     var predicates: [NSPredicate] = []
  26.     for item in set {
  27.         let query = NSPredicate(format: "%K == %@", argumentArray: [key, item])
  28.         predicates.append(query)
  29.     }
  30.     let compoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
  31.     print(compoundPredicate)
  32. //    return objects!.filter(compoundPredicate)
  33. }
  34.  
  35. struct CodableStruct: Codable {
  36.     let name: String
  37. }
  38.  
  39. let uuids: Set<UUID> = Set([UUID(), UUID()])
  40. let test = queryFromCollection(of: CodableStruct.self, query: uuids, for: "name")
  41. //let test2 = queryFromCollection2(of: CodableStruct.self, query: uuids, for: "name") //Crash
  42. let test3 = queryFromCollection3(of: CodableStruct.self, query: uuids, for: "name")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement