I have an array, with custom objects.
I Would like to pop the repeated objects, with the repeated properties:
let product = Product()
product.subCategory = "one"
let product2 = Product()
product2.subCategory = "two"
let product3 = Product()
product3.subCategory = "two"
let array = [product,product2,product3]
in this case, pop the product2 or product3
Here is an Array extension to return the unique list of objects based on a given key:
extension Array {
func unique<T:Hashable>(map: ((Element) -> (T))) -> [Element] {
var set = Set<T>() //the unique list kept in a Set for fast retrieval
var arrayOrdered = [Element]() //keeping the unique list of elements but ordered
for value in self {
if !set.contains(map(value)) {
set.insert(map(value))
arrayOrdered.append(value)
}
}
return arrayOrdered
}
}
using this you can so this
let unique = [product,product2,product3].unique{$0.subCategory}
this has the advantage of not requiring the Hashable and being able to return an unique list based on any field or combination
You can use Swift Set:
let array = [product,product2,product3]
let set = Set(array)
You have to make Product conform to Hashable (and thus, Equatable) though:
class Product : Hashable {
var subCategory = ""
var hashValue: Int { return subCategory.hashValue }
}
func ==(lhs: Product, rhs: Product) -> Bool {
return lhs.subCategory == rhs.subCategory
}
And, if Product was a NSObject subclass, you have to override isEqual:
override func isEqual(object: AnyObject?) -> Bool {
if let product = object as? Product {
return product == self
} else {
return false
}
}
Clearly, modify those to reflect other properties you might have in your class. For example:
class Product : Hashable {
var category = ""
var subCategory = ""
var hashValue: Int { return [category, subCategory].hashValue }
}
func ==(lhs: Product, rhs: Product) -> Bool {
return lhs.category == rhs.category && lhs.subCategory == rhs.subCategory
}
If Product conforms to Equatable, where a product is equal based on it's subcategory (and you don't care about order), you can add the objects to a set, and take an array from that set:
let array = [product,product2,product3]
let set = NSSet(array: array)
let uniqueArray = set.allObjects
or
let array = [product,product2,product3]
let set = Set(array)
let uniqueArray = Array(set)
If your class conforms to protocol Hashable and you would like to keep the original array order you can create an extension as follow:
extension Array where Element: Hashable {
var uniqueElements: [Element] {
var elements: [Element] = []
for element in self {
if let _ = elements.indexOf(element) {
print("item found")
} else {
print("item not found, add it")
elements.append(element)
}
}
return elements
}
}
class Product {
var subCategory: String = ""
}
let product = Product()
product.subCategory = "one"
let product2 = Product()
product2.subCategory = "two"
let product3 = Product()
product3.subCategory = "two"
let array = [product,product2,product3]
extension Product : Hashable {
var hashValue: Int {
return subCategory.hashValue
}
}
func ==(lhs: Product, rhs: Product)->Bool {
return lhs.subCategory == rhs.subCategory
}
let set = Set(array)
set.forEach { (p) -> () in
print(p, p.subCategory)
}
/*
Product one
Product two
*/
if an item is part of set or not doesn't depends on hashValue, it depends on comparation. if your product conform to Hashable, it should conform to Equatable. if you need that the creation of the set depends solely on subCategory, the comparation should depends solely on subCategory. this can be a big trouble, if you need to compare your products some other way
Here is a KeyPath based version of the Ciprian Rarau' solution
extension Array {
func unique<T: Hashable>(by keyPath: KeyPath<Element, T>) -> [Element] {
var set = Set<T>()
return self.reduce(into: [Element]()) { result, value in
guard !set.contains(value[keyPath: keyPath]) else {
return
}
set.insert(value[keyPath: keyPath])
result.append(value)
}
}
}
example usage:
let unique = [product, product2, product3].unique(by: \.subCategory)
Related
I'm working with a Set of CarCagetory:
public struct Images {
static let categoryTeaser = UIImage()
}
public enum PremiumModel {
case model1, model2, model3, unknown
}
public struct CarCategory {
public let teaserImage: UIImage
public let make: String
public let model: PremiumModel
public let startPrice: Double
}
// MARK: - Equatable
extension CarCategory: Equatable {
public static func == (lhs: CarCategory, rhs: CarCategory) -> Bool {
return lhs.model == rhs.model
}
}
// MARK: - Hashable
extension CarCategory: Hashable {
public var hashValue: Int {
return model.hashValue
}
}
I'm iterating over an array of Cars and extracting a Set of categories according to the model:
var categories = Set<CarCategory>()
carSpecifications.forEach {
if PremiumModel(rawValue: $0.car.model) != .unknown {
categories.insert(CarCategory(teaserImage: Images.categoryTeaser, make: $0.car.make, model: PremiumModel(rawValue: $0.car.model), startPrice: $0.pricing.price))
}
}
This works just fine.
Now I want to keep my Set updated with the lowest price for a certain model. I'm thinking on a dictionary of [PremiumModel: Double] where I keep the lowest price for a model and at the end I update my Set accordingly, but I wonder if there is a better way.
Edit:
That's my current implementation using a dictionary. It feels rudimentary...
carSpecifications.forEach {
let model = PremiumModel(rawValue: $0.car.model)
if model != .unknown {
if let value = minPriceForModel[model] {
minPriceForModel[model] = min(value, $0.pricing.price)
} else {
minPriceForModel[model] = $0.pricing.price
}
categories.insert(CarCategory(teaserImage: Images.categoryTeaser, make: $0.car.make, model: model, startPrice: $0.pricing.price))
}
}
let sortedCategories = Array(categories.sorted(by: <))
.compactMap { (category: CarCategory) -> CarCategory in
var newCategory = category
newCategory.startPrice = minPriceForModel[category.model] ?? 0
return newCategory
}
return sortedCategories
I have struct model like
struct ModelA {
let text: String
let id: Int
}
extension ModelA: Equatable {}
func ==(lhs: ModelA, rhs: ModelA) -> Bool {
let areEqual = lhs.id == rhs.id
return areEqual
}
i have created arrays of this model
let a1:[ModelA] = [ModelA(text: "10", id: 11), ModelA(text: "11", id: 12)]
let a2:[ModelA] = [ModelA(text: "11", id: 12)]
and having a comparing function
func isEqualArray(first array1: [Any], second array2: [Any]) -> Bool {
let set1 = NSSet(array: array1)
let set2 = NSSet(array: array2)
return set1.isSubset(of: set2 as! Set<AnyHashable>)
}
so when i'm trying to cross check
let flag = isEqualArray(first: a1, second: a2)
print("### \(flag)")
It crashes on function return
What am I doing wrong?
Your struct needs to conform to both equatable and hashable in order to be used in a Set. It seems that you only care about the id, so a simple implementation would be:
struct ModelA {
let text: String
let id: Int
}
extension ModelA: Equatable {
static func ==(lhs: ModelA, rhs: ModelA) -> Bool {
return lhs.id == rhs.id
}
}
extension ModelA: Hashable {
var hashValue: Int {
return id
}
}
Now, you can use Swift sets in your isEqualArray function; you also need to consider which set is smaller since you are using isSubSet(of:):
func isEqualArray(first array1: [AnyHashable], second array2: [AnyHashable]) -> Bool {
let set1: Set<AnyHashable>
let set2: Set<AnyHashable>
if array1.count > array2.count {
set1 = Set(array1)
set2 = Set(array2)
} else {
set1 = Set(array2)
set2 = Set(array1)
}
return set2.isSubset(of: set1)
}
Your code actually determines if one array is a subset of another, not if the arrays are equal, so I am not sure if that is what you want.
I have a custom class defined as follows :
class DisplayMessage : NSObject {
var id : String?
var partner_image : UIImage?
var partner_name : String?
var last_message : String?
var date : NSDate?
}
Now I have an array myChats = [DisplayMessage]?. The id field is unique for each DisplayMessage object. I need to check my array and remove all duplicates from it, essentially ensure that all objects in the array have a unique id. I have seen some solutions using NSMutableArray and Equatable however I'm not sure how to adapt them here; I also know of Array(Set(myChats)) however that doesn't seem to work for an array of custom objects.
Here is an Array extension to return the unique list of objects based on a given key:
extension Array {
func unique<T:Hashable>(map: ((Element) -> (T))) -> [Element] {
var set = Set<T>() //the unique list kept in a Set for fast retrieval
var arrayOrdered = [Element]() //keeping the unique list of elements but ordered
for value in self {
if !set.contains(map(value)) {
set.insert(map(value))
arrayOrdered.append(value)
}
}
return arrayOrdered
}
}
for your example do:
let uniqueMessages = messages.unique{$0.id ?? ""}
You can do it with a set of strings, like this:
var seen = Set<String>()
var unique = [DisplayMessage]
for message in messagesWithDuplicates {
if !seen.contains(message.id!) {
unique.append(message)
seen.insert(message.id!)
}
}
The idea is to keep a set of all IDs that we've seen so far, go through all items in a loop, and add ones the IDs of which we have not seen.
Here is an Array extension to return the unique list of objects based on a keyPath:
extension Array {
func uniques<T: Hashable>(by keyPath: KeyPath<Element, T>) -> [Element] {
return reduce([]) { result, element in
let alreadyExists = (result.contains(where: { $0[keyPath: keyPath] == element[keyPath: keyPath] }))
return alreadyExists ? result : result + [element]
}
}
}
Usage:
myChats.uniques(by: \.id)
Create a free duplicate version of an Array, using equality comparisons based on a given key
public extension Sequence {
public func uniq<Id: Hashable >(by getIdentifier: (Iterator.Element) -> Id) -> [Iterator.Element] {
var ids = Set<Id>()
return self.reduce([]) { uniqueElements, element in
if ids.insert(getIdentifier(element)).inserted {
return uniqueElements + CollectionOfOne(element)
}
return uniqueElements
}
}
public func uniq<Id: Hashable >(by keyPath: KeyPath<Iterator.Element, Id>) -> [Iterator.Element] {
return self.uniq(by: { $0[keyPath: keyPath] })
}
}
public extension Sequence where Iterator.Element: Hashable {
var uniq: [Iterator.Element] {
return self.uniq(by: { (element) -> Iterator.Element in
return element
})
}
}
Usage
let numbers = [1,2,3,4,5,6,7,1,1,1,]
let cars = [Car(id:1), Car(id:1), Car(id:2)]
numbers.uniq
cars.uniq(by: { $0.id})
cars.uniq(by: \Car.id)
cars.uniq(by: \.id)
I have a class called Person:
class Person{
let name: String?
let areas: [Area]
}
And Area consisting of the following:
class Area{
let id: String
let name: String
}
I've created a "people" array and what I want to do is check the "people" array if area.name = "Brooklyn". If it doesn't remove that person from the "people" array.
Here is what I have tried but it doesn't work:
var people = [Person]()
for s in people{
for a in s.preferredArea{
if a.areaName != "Brooklyn"{
let index = people.indexOf(s)
people.removeAtIndex(index!)
}
}
}
Can anyone point me into the right direction please?
To use indexOf, you'll have to make Person conforming the protocol Equatable:
class Person: Equatable {
let name: String
var area: [Area]
init(name: String) {
self.name = name
self.area = []
}
}
func ==(lhs: Person, rhs: Person) -> Bool {
return lhs.name == rhs.name &&
lhs.area == rhs.area // You'll have to make Area equatable too
}
Having this done, your code just works fine. If you don't want to use this method, you can use:
people.enumerate().forEach {
if !$0.1.area.contains({ $0.name == "Brooklyn" }) {
people.removeAtIndex($0.0)
}
}
Or if you can use filter to create a new array:
var newPeople = people.filter { $0.area.contains { $0.name == "Brooklyn" } }
I have two arrays, they are of two different objects, and both contain an ID field. What I need to do is display them in order in a table view controller. They share the same basic info, Size and ID, and those are the only pieces of data displayed, in addition to the type of object it is. When the user selects a cell then it moves to a new view that displays the finer details of the object.
Right now, I have two sections in the table, one for TypeA, and the other for TypeB. They sort through all of the items in their respective list, but are out of order for when the item was made. So it looks like:
TypeA
ID 1
ID 2
ID 5
ID 6
TypeB
ID 3
ID 4
ID 7
What I need is for it to sort them all into 1 section, and still open the detail view when selected.
Thoughts
I could put them all into an AnyObject dictionary and when looking at individual items determine if they are of one object type or the other. I feel like that would work, but how would I go about sorting that correctly?
Put all common properties into a protocol, the build and sort an array of that common protocol:
protocol HasID {
var id: Int { get }
}
class TypeA : HasID, CustomStringConvertible {
var id: Int
init(_ id: Int) {
self.id = id
}
var description : String {
return ("TypeA(\(self.id))")
}
}
class TypeB : HasID, CustomStringConvertible {
var id: Int
init(_ id: Int) {
self.id = id
}
var description : String {
return ("TypeB(\(self.id))")
}
}
let typeA = [TypeA(1), TypeA(2), TypeA(5), TypeA(6)]
let typeB = [TypeB(3), TypeB(4), TypeB(7)]
let result: [HasID] = (typeA + typeB).sorted { $0.id < $1.id }
print(result)
[TypeA(1), TypeA(2), TypeB(3), TypeB(4), TypeA(5), TypeA(6), TypeB(7)]
Alternatively to Zoff Dino answer if you do not want to burden TypeA and TypeB classes with HasID protocol then you can define extension to these classes in your view controller:
class TypeA {
var ID: Int
init(_ id: Int) {
self.ID = id
}
}
class TypeB {
var ID: Int
init(_ id: Int) {
self.ID = id
}
}
protocol HasID {
var ID: Int { get }
}
// place this in your view controller
extension TypeA: HasID {
}
extension TypeB: HasID {
}
var arrayA = [TypeA(1), TypeA(3), TypeA(5)]
var arrayB = [TypeB(2), TypeB(4)]
let sortedArray = (arrayA.map { $0 as HasID } + arrayB.map { $0 as HasID })
.sort { $0.ID < $1.ID }
You can do this like so:
class TypeA {
var ID: Int
init(_ id: Int) {
self.ID = id
}
}
class TypeB {
var ID: Int
init(_ id: Int) {
self.ID = id
}
}
struct Wrap {
var ID: Int {
return a?.ID ?? b?.ID ?? 0
}
let a: TypeA?
let b: TypeB?
}
var arrayA = [TypeA(1), TypeA(3), TypeA(5)]
var arrayB = [TypeB(2), TypeB(4)]
let sortedArray = (arrayA.map { Wrap(a: $0, b: nil) } + arrayB.map { Wrap(a: nil, b: $0)})
.sorted { $0.ID < $1.ID }
When row is selected you can determine object with:
if let a = sortedArray[index].a {
// TypeA row selected
} else if let b = sortedArray[index].b {
// TypeB row selected
}