Advertisement
HXXXXJ

filter server response - only add

Mar 12th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.66 KB | None | 0 0
  1. struct Entry {
  2.     let id : String
  3.     var checked : Bool
  4.     init(_ dic : [String: String]) {
  5.         id = dic["id"]!
  6.     }
  7. }
  8. //add to local array if not exist on local
  9. func addNewEntry(_ response : [[String : String]], _ localArr: inout [Entry] ){
  10.    
  11.     let localIDset = Set<String>(localArr.reduce([String]()){ $0 + [$1.id] })
  12.     for item in response{
  13.         if !localIDset.contains(item["id"]!){
  14.             localArr.append(Entry(item))
  15.         }
  16.     }
  17. }
  18.  
  19. // remove on local array if not exist on server
  20.  
  21.  
  22. let resp = [["id" : "123"],["id" : "3"],["id" : "23"]]
  23. var localArray = [Entry]()
  24.  
  25. addNewEntry(resp, &localArray)
  26.  
  27. print(localArray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement