Advertisement
Larme

Untitled

Oct 28th, 2020
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.75 KB | None | 0 0
  1. let keyword: String? = ""
  2. let type: String?  = ""
  3. let use: String?  = ""
  4. let max: Int?  = 0
  5. let min: Int? = 4
  6.  
  7. let orgsearchDataModel: [Datum] = []
  8.  
  9. extension String {
  10.     func contains(_ string: String, caseInsenstive: Bool = true) -> Bool {
  11.         return range(of: string, options: caseInsenstive ? .caseInsensitive : []) != nil
  12.     }
  13. }
  14.  
  15. extension Datum {
  16.     func matchAddress(_ string: String?) -> Bool {
  17.         guard let string = string else { return true }
  18.         guard let address = listingAddress else { return true }
  19.         return address.contains(string)
  20.     }
  21.     func matchType(_ string: String?) -> Bool {
  22.         guard let string = string else { return true }
  23.         return listingType.contains(string)
  24.     }
  25.     func matchUse(_ string: String?) -> Bool {
  26.         guard let string = string else { return true }
  27.         return listingUse.contains(string)
  28.     }
  29.     func isIncludedBetween(min: Int?, max: Int?) -> Bool  {
  30.         guard let listingPrice = listingPrice, let price = Int(listingPrice) else { return false }
  31.         return price >= (min ?? Int.min) && price <= (max ?? Int.max)
  32.     }
  33. }
  34.  
  35.  
  36.  
  37. let filtered = orgsearchDataModel.filter {
  38.     return $0.matchAddress(keyword) && $0.matchUse(use) && $0.matchType(type) && $0.isIncludedBetween(min: min, max: max)
  39. }
  40.  
  41. let filter2 = orgsearchDataModel.filter {
  42.     let firstTest = $0.listingAddress!.range(of: keyword!, options: .caseInsensitive) != nil && $0.listingType.range(of: type!, options: .caseInsensitive) != nil && $0.listingUse.range(of: use!, options: .caseInsensitive) != nil
  43.     guard firstTest == true else { return false }
  44.     if let price = Int($0.listingPrice ?? "0") {
  45.         return (price > min! && price < max!) && firstTest
  46.     }
  47.     return false
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement