3

I am filtering an array that can have a value where there are multiple Models of the same name, only they have different model numbers.

Variables

var modelArray = [model]()

Struct

struct model {
    var modelName = String();
    var modelNumber = String();
    var manufacturer = String();
    var phiTypeCode = String();
    var phiTypeDesc = String();
}

Filter

var filteredArray = self.modelArray.filter { $0.manufacturer.range(of: manufacturerVar, options: .caseInsensitive) != nil }

This produces the correct filtered Array, only due to the possibility of similar models with different model numbers, I am trying to remove duplicates from filteredArray. Fairly new to swift I don't have a great deal of experience making the struct hashable to be able to use the suggested solutions.

Hopefully this is more clear

JLanders
  • 101
  • 2
  • 11
  • How are you saving the data? Where is the data coming from? – MwcsMac Feb 17 '17 at 17:37
  • 3
    "I am getting duplicate values" It's easy to eliminate duplicate values, but what do you mean "I am getting"? You are _putting_ the duplicate values there. If you don't want them there, don't do that. – matt Feb 17 '17 at 17:37
  • @JLanders On an unrelated note, does it make sense to have a model called `""`, manufactured by `""`, and with no phyType code or Desc? – Alexander Feb 17 '17 at 17:38
  • As it stands, how is this not a duplicate of http://stackoverflow.com/questions/38153674/remove-duplicate-structs-in-array-based-on-struct-property-in-swift?rq=1 ? – matt Feb 17 '17 at 17:39
  • 1
    Possible duplicate of [Does there exist within Swift's API an easy way to remove duplicate elements from an array?](http://stackoverflow.com/questions/25738817/does-there-exist-within-swifts-api-an-easy-way-to-remove-duplicate-elements-fro) – Hamish Feb 17 '17 at 17:39
  • @Hamish I did look at that solution, However fairly new to Swift, was having difficulty make my Struct Hashable to work in that solution – JLanders Feb 17 '17 at 17:53
  • @JLanders There is [an answer](http://stackoverflow.com/a/33984316/2976878) to that linked Q&A which gives an example of conforming to `Hashable` – also see [Make struct Hashable?](http://stackoverflow.com/questions/41972319/make-struct-hashable) & [How to implement the Hashable Protocol in Swift for an Int array (a custom string struct)](http://stackoverflow.com/q/31438210/2976878), which contains many useful links. – Hamish Feb 17 '17 at 18:12
  • Making your own functionality to generate a unique data source if pretty easy, using `fast enumeration` or `mapping` plus conforming to protocol `Equatable`. See my answer. – Glenn Posadas Feb 17 '17 at 18:16

1 Answers1

14

First off, I tried making a sample in my PlayGround.

  1. Conform your model model to the protocal Equatable, like so:

    struct Car: Equatable {
    
        var modelName = String()
        var manufacturer = String()
    
        init(modelName: String, manufacturer: String) {
            self.modelName = modelName
            self.manufacturer = manufacturer
        }
    
        static func == (lhs: Car, rhs: Car) -> Bool {
            return lhs.modelName == rhs.modelName
        }
    }
    

In the code above, we're assuming that the modelName is the primary key of your model.

  1. Now make a function that enumerates your data source and returns a new data source after checking the element one by one, like so:

    // returns unique array
    
    func unique(cars: [Car]) -> [Car] {
    
        var uniqueCars = [Car]()
    
        for car in cars {
            if !uniqueCars.contains(car) {
                uniqueCars.append(car)
            }
        }
    
        return uniqueCars
    }
    

Finally, you now have the function to generate a new unique data source.

Example:

// Variable

var cars = [Car]()

// Adding data models to data source

let car1 = Car(modelName: "Kia Picanto", manufacturer: "Kia")
let car2 = Car(modelName: "Honda XX", manufacturer: "Honda")
let car3 = Car(modelName: "Honda XX", manufacturer: "Honda")

cars.append(car1)
cars.append(car2)
cars.append(car3)

// Now contains only two elements.
let uniqueCars = unique(cars: cars)
Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95