0

Im trying to avoid duplicates in my object array tat returns from core-data, by using an extension. But it gives me the error saying "cannot convert the value type of ()-> to expected argument type [Object type]->()". my code as bellow. What am i missing.

    viewDidLoad (){
     // get the above mentioned error here
     let uniqueArray = myArray.unique{$0.Id}
    }

   extension Array {
     func unique<T:Hashable>(map: ((Element) -> (T))) -> [Element] {
     var set = Set<T>() /
     var arrayOrdered = [Element]() 

      for value in self {
        if !set.contains(map(value)) {
        set.insert(map(value))
        arrayOrdered.append(value)
        }
      }

     return arrayOrdered
   }
  } 
danu
  • 1,079
  • 5
  • 16
  • 48
  • Your code works for me, you might wanna post your `myArray`'s object class here to see what else is wrong – Tj3n Apr 20 '18 at 03:15
  • Also to remove duplicates you can just create an Set from the Array then convert the set back to an Array instead of iterating over the array elements. [See here](https://stackoverflow.com/a/29904817/312312) – Lefteris Apr 20 '18 at 05:30
  • My array is returning from coreData. Basically its an object array where i wants to compare a key – danu Apr 20 '18 at 06:32

0 Answers0