Array+Extension.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // Array+Extension.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/08/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. // Found in Internet
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. import Foundation
  25. import UIKit
  26. // https://stackoverflow.com/questions/33861036/unique-objects-inside-a-array-swift/45023247#45023247
  27. extension Array {
  28. func unique<T: Hashable>(map: ((Element) -> (T))) -> [Element] {
  29. var set = Set<T>() // the unique list kept in a Set for fast retrieval
  30. var arrayOrdered = [Element]() // keeping the unique list of elements but ordered
  31. for value in self where !set.contains(map(value)) {
  32. set.insert(map(value))
  33. arrayOrdered.append(value)
  34. }
  35. return arrayOrdered
  36. }
  37. }
  38. extension Array where Element == URLQueryItem {
  39. subscript(name: String) -> URLQueryItem? {
  40. first(where: { $0.name == name })
  41. }
  42. }