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

8
  • How are you saving the data? Where is the data coming from? Commented Feb 17, 2017 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. Commented Feb 17, 2017 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? Commented Feb 17, 2017 at 17:38
  • As it stands, how is this not a duplicate of stackoverflow.com/questions/38153674/… ? Commented Feb 17, 2017 at 17:39
  • 1
    Possible duplicate of Does there exist within Swift's API an easy way to remove duplicate elements from an array? Commented Feb 17, 2017 at 17:39

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

This was perfect!! Thank you for taking the time to detail this out
Sure, you're welcome. Now you can use the same function contains before adding an element to your data source. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.